为什么javascript不生成函数的输出?
为什么javascript不生成函数的输出为什么javascript不生成函数的输出?,javascript,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
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>
var name=prompt(“您叫什么名字?”);
var-weight=提示(“您的体重是多少?”);
功能检查(名称、重量){
如果(重量>25){
提醒(姓名+“您的体重是”+“体重+”,这不正常。”+“对不起”);
}
否则{
提醒(姓名+“您的体重是”+“体重+”,这是正常的。“+”欢迎”);
}
退货检查;
}检查;
因为它的格式不正确,所以您甚至不结束函数
必须返回一个值才能返回任何内容
也许这就是你想要实现的目标:
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));
请注意,我是如何将参数
name
和weight
传递到check
-函数的,它应该是这样的:
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
声明函数时,您正在声明参数:name和weight。这些参数只能在该函数的范围内使用。看起来您正试图将它们设置在范围之外。然后返回变量check。它是空的。此外,您没有从函数外部调用函数,因此永远不会触发函数check() 这里有一个例子():
var input1=“peter”;输入2=10;
检查(输入1,输入2);
功能检查(名称、重量){
var检查;
如果(重量<25){//true
check=“Text”+名称;
}否则{
勾选=“无文本”+名称;
}
返回警报(检查);
}
PS:尚未测试此代码。函数需要外部调用来执行操作。因此,如果你想显示结果,你可以用上面的方式添加
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>
功能检查(名称、重量){
如果(重量>25){
提醒(姓名+“您的体重是”+“体重+”,这不正常。”+“对不起”);
}
否则{
提醒(姓名+“您的体重是”+“体重+”,这是正常的。“+”欢迎”);
}
退货检查;
}
var name=prompt(“您叫什么名字?”);
var-weight=提示(“您的体重是多少?”);
document.getElementById(“print”).innerHTML=检查(名称、重量);
1。因为你有一个语法错误。2.因为你没有调用你定义的函数。你似乎没有调用check。此外,您的函数未关闭请再次检查,编辑您必须通过参数检查(名称、重量)代码>返回警报(…)代码>没有什么意义,不是吗?
<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>