[英]JS console any option from if...else
I´ve started with JS, and I cannot get print in console, one of the following options in this exercise.我从 JS 开始,我无法在控制台中打印,本练习中的以下选项之一。 The selected option depends on the input of the user with a prompt, but after reviewing everything I cannot debug it, Any help please??选择的选项取决于用户的输入提示,但在查看所有内容后我无法调试它,请问有什么帮助吗? Thank you!!谢谢!!
let guests = prompt('How many people are coming to your wedding?');
function getPrice(guests) {
let cost = 0;
if (guests <= 50) {
cost = 4000;
}
else if (guests >= 51){
cost = 10000;
}
else if (guests >= 101){
cost = 15000;
}
else (guests >= 201){
cost = 20000;
}
return cost;
}
let price = getPrice();
console.log ('Your wedding will cost '+'$'+price+' dollars');
the main problem you encounter is that in your function definition you have an argument guests
but you run the function without it getPrice()
so guests will be undefined because you defined it for function scope.您遇到的主要问题是,在您的函数定义中,您有一个参数guests
但是您在没有getPrice()
情况下运行该函数,因此来宾将是未定义的,因为您为函数范围定义了它。
you have two options now,你现在有两个选择
1: just remove the argument from defintion and it will take the value of the "global scope" variable guests
1:只需从定义中删除参数,它将采用“全局范围”变量guests
function getPrice() {...
2: just pass guests like let price = getPrice(guests)
2: 像let price = getPrice(guests)
这样传递客人
the if else part i guess will be easy to fix then ;) if else 部分我想这很容易解决;)
here you see working option 1:在这里您可以看到工作选项 1:
let guests = prompt('How many people are coming to your wedding?'); function getPrice() { let cost = 0; if (guests >= 201){ cost = 20000; } else if (guests >= 101){ cost = 15000; } else if (guests >= 51){ cost = 10000; } else { cost = 4000; } return cost; } let price = getPrice(); console.log ('Your wedding will cost '+'$'+price+' dollars');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.