如果使用Switch替换其他链:返回后无法访问

时间:2018-03-19 16:44:55

标签: javascript

function chainToSwitch(val) {
  var answer = "";
  // Only change code below this line
  switch (val) {
    case "bob":
      answer = "Marley";
      break;
    case 42: 
      answer = "The answer";
      break;
    case 1:
      answer = "There is no #1";
      break;
    case 99:
      answer = "Missed me by this much!";
      break;
    case 7:
      answer = "Ate Nine";
      break;
    default: 
      answer = "not correct";
  }
}

  // Only change code above this line  
  return answer; 


// Change this value to test
chainToSwitch(7);

我仍然不明白为什么它无法访问。我正在做FreeCodeCamp课程而且我被困在这个课程上。我甚至进入了答案的源代码,它仍然不适合我。

1 个答案:

答案 0 :(得分:2)

您需要在函数内部移动return语句。



function chainToSwitch(val) {
  var answer = "";
  // Only change code below this line
  switch (val) {
    case "bob":
      answer = "Marley";
      break;
    case 42: 
      answer = "The answer";
      break;
    case 1:
      answer = "There is no #1";
      break;
    case 99:
      answer = "Missed me by this much!";
      break;
    case 7:
      answer = "Ate Nine";
      break;
    default: 
      answer = "not correct";
  }
  return answer; 
}

console.log(chainToSwitch(7));