2014-11-24 38 views
0

爲什麼javascript不能生成函數的輸出?爲什麼JavaScript不能生成函數的輸出?

var name = prompt("What is your name?"); 
var weight = prompt("what is your weight?"); 
function check(name ,weight){ 
     if (weight> 25) { 
      alert (name + "Your weight is " + weight + "which is not normal."+ "Sorry"); 
     } 
     else { 
      alert (name + "Your weight is " + weight + "which is normal." + "Welcome"); 
     } 

    return check; 
}check; 
</script> 
+4

1.因爲你有一個語法錯誤調用它。 2.因爲你沒有調用你定義的函數。 – icktoofay 2014-11-24 06:50:53

+1

你似乎不打電話檢查。你的功能還沒關閉 – 2014-11-24 06:50:55

+0

請再檢查一次,編輯 – Rumana 2014-11-24 06:54:13

回答

1

因爲它格式不正確,您甚至不會結束該功能。

你必須返回一個值才能返回任何東西。

也許這就是你想要達到的目標:

var name = prompt("What is your name?"); 
var weight = prompt("what is your weight?"); 

function check(name ,weight){ 
    if (weight> 25) { 
    return name + "Your weight is " + weight + "which is not normal."+ "Sorry"; 
    } else { 
    return name + "Your weight is " + weight + "which is normal." + "Welcome"; 
    } 
} 

alert(check(name, weight)); 

注意我是如何傳遞參數nameweightcheck -function

1

它應該是這樣的:

 var name = prompt("What is your name?"); 
     var weight = prompt("what is your weight?"); 

     function check(name ,weight){ 
      if (weight> 25) { 
       alert (name + "Your weight is " + weight + "which is not normal."+ "Sorry"); 
      } 
      else { 
       alert (name + "Your weight is " + weight + "which is normal." + "Welcome"); 
      } 
     return true;    // check is invalid 
     }       // you forgot to close it 
     check(name,weight);   // calling the function 
0

當聲明一個函數時,你聲明的參數是:name and weight噸。這些參數只能在該函數的範圍內使用。看起來你正試圖將它們設置在範圍之外。然後你返回變量檢查。這是空的。你也不是從函數外部調用你的函數,所以函數check()永遠不會被觸發。

這裏一個例子(live example):

var input1="peter"; input2=10; 
check(input1, input2); 
function check(name, weight) { 
    var check; 
    if (weight < 25) { //true 
     check = "Text"+name; 
    } else { 
     check = "No text"+name; 
    } 
    return alert(check); 
} 

PS:沒有測試此代碼尚未。

+0

'返回警報(...);'意義不大,不是嗎? – algorhythm 2014-11-24 07:35:06

+0

您的代碼無法正常工作 – Rumana 2014-11-24 09:02:18

+0

@Rumana,代碼工作正常[請參閱此jsfiddle](http://jsfiddle.net/aum07c6b/) – Fleuv 2014-11-24 09:08:50

0

該函數需要一個外部調用來執行該操作。所以,如果你想顯示你可以在上面做的方式將結果添加

function check(name ,weight){ 
     if (weight> 25) { 
      alert (name + "Your weight is " + weight + "which is not normal."+ "Sorry"); 
     } 
     else { 
      alert (name + "Your weight is " + weight + "which is normal." + "Welcome"); 
     } 
     return true;    
    } 

    check(name,weight); 

也可以通過其他方式

<p id="print"></p> 
<script> 
    function check(name ,weight){ 
     if (weight> 25) { 
      alert (name + "Your weight is " + weight + "which is not normal."+ "Sorry"); 
     } 
     else { 
      alert (name + "Your weight is " + weight + "which is normal." + "Welcome"); 
     } 
     return check;    
    }     

    var name = prompt("What is your name?"); 
    var weight = prompt("what is your weight?"); 

    document.getElementById("print").innerHTML = check(name, weight); 
</script> 
相關問題