JS计算错误点击数以确定百分比

时间:2018-06-20 16:57:50

标签: javascript loops counter

因此,我的最终目标是能够为酒店和用户计算媒人。我将向用户提问,通过提示进行输入,我将使用它们来匹配我提供的酒店详细信息并输出,以显示最适合用户的酒店。目前我的查询是:

//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");
}

但是,即使尝试了论坛上发布的类似内容,我也很难弄清楚如何始终计算出“假”被击中的次数,以便我可以对它们进行计数并给出一个百分比。 / p>

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

1 个答案:

答案 0 :(得分:0)

使用counter代替true / false标志:

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");
}