繁体   English   中英

JS 控制台来自 if...else 的任何选项

[英]JS console any option from if...else

我从 JS 开始,我无法在控制台中打印,本练习中的以下选项之一。 选择的选项取决于用户的输入提示,但在查看所有内容后我无法调试它,请问有什么帮助吗? 谢谢!!

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');

您遇到的主要问题是,在您的函数定义中,您有一个参数guests但是您在没有getPrice()情况下运行该函数,因此来宾将是未定义的,因为您为函数范围定义了它。

你现在有两个选择

1:只需从定义中删除参数,它将采用“全局范围”变量guests

function getPrice() {...

2: 像let price = getPrice(guests)这样传递客人

if else 部分我想这很容易解决;)

在这里您可以看到工作选项 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM