简体   繁体   English

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

[英]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.

相关问题 JavaScript 错误的 if...else 逻辑 - JavaScript wrong if...else logic 为什么 Javascript `if...else if` 不以 `else` 结尾? - Why would Javascript `if...else if` not end with an `else`? Javascript 中的 If...else 语句抛出 TypeError - If...else statement in Javascript throws an TypeError 如何控制台记录JS对象的选项? - How to console log an option of a JS object? 如何在 Node js 中通过控制台日志显示选项列表并要求用户通过控制台日志从显示的列表中输入一个选项 - How is it possible in Node js to display list of options via console log and ask user to input a option from the displayed list via console log JS中是否可以检测是否从浏览器的控制台运行了任何JS? - Is it possible in JS to detect whether any JS was run from the browser's console? 如果可能,尽量减少使用 if...else 结构以使用某些设计模式为 object 属性获取正确的值 - Minimize use of if...else constructs to get the right value for an object property with some design pattern if possible 如果选择了组中的任何选项,则应使用 Ant 设计(OptGroup,选项)-React.js 禁用其他组中的所有选项 - If any option from a Group is selected, then all options in other group should get disabled using Ant design (OptGroup, Option) - React.js 多选 js 附加 getJson 不显示任何选项 - Multiselect js append with getJson not displaying any option 从控制台阻止js攻击 - block js attack from console
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM