首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >JavaScript条件语句if/else /else

JavaScript条件语句if/else /else
EN

Stack Overflow用户
提问于 2022-07-01 23:59:24
回答 1查看 43关注 0票数 -5

大家好!你能帮帮我吗?我试图利用某些条件,但由于某些原因,这些条件似乎被忽视了。当我运行该代码时,给出的弹出式随机数为93,它符合第一个声明语句(if),但是,即使在true && true时,它也被忽略并移到最后一个语句。我不明白为什么..。?

代码语言:javascript
复制
function loveMatching (name1, name2) {
    
    name1 = prompt ("Enter your name!");
    name2 = prompt ("Enter your crush name!");

   
 if (matchingPercentage() >= 70 && matchingPercentage() <=100) {

   document.write(" The compability between: " + name1 + " and " + name2 + " is of a " + matchingPercentage() + "%. You guys are meant to be together!");

  }
  else if( matchingPercentage() >=30 && matchingPercentage() <70) {
    document.write(" The compability between: " + name1 + " and " + name2 + " is of a " + matchingPercentage() + "%. Too close to fail!");
  }

 else {
  document.write(" The compability between: " + name1 + " and " + name2 + " is of a " + matchingPercentage() + "%. You better look in another direction!");
 }

  }

function matchingPercentage() {
    var n = Math.random();
    var n = Math.floor(n * 100) + 1;
return n;
}



loveMatching();
EN

回答 1

Stack Overflow用户

发布于 2022-07-02 00:03:09

每次检查它时,您都要计算一个新的匹配%,在同一条件下多次计算。一开始你只需要做一次:

代码语言:javascript
复制
function loveMatching (name1, name2) {
    
    name1 = prompt ("Enter your name!");
    name2 = prompt ("Enter your crush name!");
    const matchPercent = matchingPercentage(); // call one time
   
 if (matchPercent >= 70 && matchPercent <=100) {

   document.write(" The compability between: " + name1 + " and " + name2 + " is of a " + matchPercent + "%. You guys are meant to be together!");

  }
  else if( matchPercent >=30 && matchPercent <70) {
    document.write(" The compability between: " + name1 + " and " + name2 + " is of a " + matchPercent + "%. Too close to fail!");
  }

 else {
  document.write(" The compability between: " + name1 + " and " + name2 + " is of a " + matchPercent + "%. You better look in another direction!");
 }

}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72835555

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档