2014-02-25 118 views
-3

我希望能夠在下面的switch語句中返回每個review,此時只返回第一個review。可能是什麼問題呢?Switch中的返回語句

var getReview = function (food) {   
    switch (food) {  
     case "Pancakes": 
      console.log("The Kids love it"); 
      break;   
     case "Pasta": 
      console.log("Popular Italian food"); 
      break;     
     case "Naan": 
      console.log("Asian bread"); 
      break;    
     default: 
      console.log("Don't like food?"); 
      break; 
    } 

    return food; 
}; 
+3

你是如何調用'getReview()'? –

+0

getReview是函數名稱 – 1088

+3

當前,函數只是返回傳入的值,因此不會返回審閱。 Sayem在他的問題中所指的意思是,他希望你向我們展示代碼的功能。 – JLRishe

回答

3
var getReview = function (food) { 
    var review; 
    switch (food) { 

     case "Pancakes": 
      review = "The Kids love it"; 
      break; 

     case "Pasta": 
      review = "Popular Italian food"; 
      break; 


     case "Naan": 
      review = "Asian bread"; 
      break; 

     default: 
      review = "Don't like food?"; 
      break; 
    } 
    console.log(review); 
    return review; 
}; 

var review = getReview('Pancakes'); //Return value = The Kids love it, Console = The Kids love it 
+1

你可以在'switch'語句後加上'console.log'。不需要在每個「case」塊中都有。 – JLRishe

+0

哦,當然。謝謝。我編輯過它。 – alexP

1

我不知道你想,不過,這將是有用的這樣定義的功能到底是什麼:當你有這樣的功能

var getReview = function (food) { 
    switch (food){ 
    case "Pancakes": 
    return "The Kids love it"; 
    case "Pasta": 
    return "Popular Italian food"; 
    case "Naan": 
    return "Asian bread"; 
    default: 
    return "Don't like food?"; 
    } 
}; 

,就可以得到結果像如下:

getReview('Pancakes'); 
=> 'The Kids love it' 

getReview('Paster'); 
=> 'Popular Italian food' 
...