【问题标题】:if and elseif conditions are not working in Javascriptif 和 elseif 条件在 Javascript 中不起作用
【发布时间】:2021-04-28 11:43:20
【问题描述】:

我在 JavaScript 上进行了 IQ 测试,并给出了分数的条件,在某些范围内它显示不同的 IQ 分数,但无论有多少正确答案,它都只显示最后一个条件。我已经尝试了很多东西,但没有任何效果。我需要尽快提交这个项目。如果有人可以提供帮助,我将不胜感激。我在下面附上了相关部分的代码。

  
 

 function displayResult() 
{  
    var score = $('<p>',{id: 'question'});

    var correct = 0;
    for (var i = 0; i < selectOptions.length; i++) 
    {

      if (selectOptions[i] === allQuestions[i].answer) {

        correct++ ;

        console.log(correct)
      }
    }

    if (correct < 5){
      score.append("Your IQ Score lies in the range of 70-79 which is classified as Bordeline. 6.7% of the world's population falls in this range.<br> <img id='img1' src='BORDERLINE.png' height=90px width=500px/></br>");


    }else if (correct>=5 && correct < 10){
      score.append("Your IQ Score lies in the range of 80-89 which is classified as low average. 16.1% of the world's population falls in this range. <br> <img id='img1' src='LOW AVERAGE.png' height=90px width=500px/></br> ");

    }else if (correct >= 10 && correct < 20){

      score.append("Your IQ Score lies in the range of 90-109 which is
 
 classified as average. 50% of the world's population falls in this range. <br>

 <img id='img1' src='AVERAGE.png' height=90px width=500px/></br>");

    }else if (correct >= 20 && correct <=25){

      score.append("Your IQ Score lies in the range of 110-119 which is 

        classified as high average. 16.1% of the world's population falls in 

         this range. <br> <img id='img1' src='HIGH AVERAGE.png' height=90px 
         width=500px/></br>"); 

    }else if(correct >= 25 && correct <=30) {

       score.append("Your IQ Score lies in the range of 119-129 which is 

       classified as superior. 6.7% of the world's population falls in this
 
        range. 
       <br> <img id='img1' src='SUPERIOR.png' height=90px width=500px/> 

       </br>");  }
   
          
    return score;    
   
  }
   <html>
    <head>
        <title>Make Quiz Website</title>
        <link rel="stylesheet" href="quiz.css">
        <link href="https://fonts.googleapis.com/css?family=Josefin+Sans" rel="stylesheet">
    </head>
    <body>
        <div id="container">
            <h1>Quiz Website Using JavaScript</h1>
            <br/>
            <div id="quiz"></div>
              <p id="countdown">30:00</p></h1>
            <div class="button" id="next"><a href="#">Next</a></div>
            <div class="button" id="prev"><a href="#">Prev</a></div> 
        </div>
        

        <script src="https://code.jquery.com/jquery-3.4.0.min.js"></script>
        <script src="quiz.js"></script>
    </body>
</html>

【问题讨论】:

  • 您的第一个条件correct == 0 &amp;&amp; correct &lt; 5 似乎需要使用|| 而不是&amp;&amp;,因为如果correct == 0,那么它已经是&lt;5
  • if (正确 = 0 && 正确
  • 是的,我的意思是正确的>=0

标签: javascript html jquery if-statement conditional-statements


【解决方案1】:

(correct = 0 &amp;&amp; correct &lt;= 5)设置 correct 为 0。它不会测试它针对 0。即使你的意思是 correct == 0 &amp;&amp; correct &lt;= 5,这也是多余的,因为 0 总是

也许你的意思是correct &gt;= 0 &amp;&amp; correct &lt;= 5

【讨论】:

    【解决方案2】:

    应更改此部分以使其有意义。

    if (correct = 0 && correct <= 5){
              score.append("YOUR IQ SCORES LIES IN THE RANGE OF 70 and 79 WHICH IS CLASSIFIED AS BORDERLINE <img src='img9b.png'/>");
    }
    

    类似的东西。

    if (correct <= 5) {
        score.append("YOUR IQ SCORE IS IN THE RANGE OF 70 and 79 WHICH IS CLASSIFIED AS BORDERLINE <img src='img9b.png'/>");
    } else if (correct <= 10) {
        score.append("YOUR IQ SCORE IS IN THE RANGE OF 80 and 119 WHICH IS CLASSIFIED AS AVERAGE <img src='img9b.png'/>");
    } else {
        score.append("YOUR IQ SCORE IS ABOVE AVERAGE <img src='img9b.png'/>");
    }
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-03-21
      • 2020-07-18
      • 1970-01-01
      • 1970-01-01
      • 2017-10-26
      • 1970-01-01
      • 1970-01-01
      • 2021-07-11
      相关资源
      最近更新 更多