2014-11-24 40 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> 
相关问题