【问题标题】:JS calculate false hits to determine percentageJS计算错误命中来确定百分比
【发布时间】:2018-06-20 16:57:49
【问题描述】:

所以我的最终目标是能够为酒店和用户计算媒人。我将有问题让用户通过提示输入,我将使用它来匹配我提供的酒店详细信息并输出以显示哪些酒店最适合用户。目前我的疑问是:

//Hotel choices
const minStar = prompt("What is the minimum star rating you would like?");
if (minStar==5)
    {console.log(`That sounds grand`);}
else
    {console.log(`Just ${minStar}, huh?`);}
const distCityMax = prompt("What is the furthest distance in miles from a city you would be happy with?");
console.log(`${distCityMax} miles maximum from a city`);
const wifi = prompt("Do you require WiFi?");
console.log(`Do you require wifi? ${minStar}`);
const swimPool = prompt("Is a swimming pool important?");
console.log(`Your decision on a swimming pool was: ${swimPool}`);

目前我只需要找到一个示例酒店的匹配项:

let passed=true;

if(minStar!=5){
    passed=false;
}
if(distCityMax>=1){
    passed=false;
}   
if(wifi!=="yes"){
    passed=false;
}
if(swimPoo!=="yes"){
    passed=false;
}

if(passed===true){
    console.log("These hotels would suit you: TheGrand");
}else{
    console.log("There are no matching hotels");
}

但我无法弄清楚,即使在尝试了在论坛上发布的类似内容之后,如何能够计算所有“错误”被击中的次数,以便我可以计算它们并与它们一起显示百分比。

tldr;如何计算“passed=false”的次数?

【问题讨论】:

  • 只需创建一个变量并增加计数。

标签: javascript loops counter


【解决方案1】:

使用计数器而不是真/假标志:

let count = 0;

if (minStar != 5) {
    count++;
}
if (distCityMax >= 1) {
    count++;
}
if (wifi !== "yes") {
    count++;
}
if (swimPoo !== "yes") {
    count++;
}

console.log("Percentage of 'false': " + Math.round(100 * count / 4));

if (count === 0) {
    console.log("These hotels would suit you: TheGrand");
} else {
    console.log("There are no matching hotels");
}

【讨论】:

  • 感谢这个替代且有效的解决方案!
猜你喜欢
  • 2016-06-15
  • 2016-09-04
  • 1970-01-01
  • 2013-05-05
  • 1970-01-01
  • 2018-04-14
  • 2011-06-01
  • 2021-01-10
相关资源
最近更新 更多