具有范围输入值的Javascript条件语句

时间:2018-12-17 10:53:26

标签: javascript html forms if-statement range

有人可以通过以下Javascript代码帮助我。如果“得分”的值介于1-3、3-5、5-7、7-9和9-10之间,我希望条件语句能够执行。

当前,它们仅在变量“分数”是整数时执行,但是我希望分数在数字范围之间执行。

例如,我希望“ if((score == 1)||(score == 2)||(score == 3))”行类似于if(分数在1到3之间) ,然后将执行该语句。

这是HTML:

Input 1 <input type="number" id="num1">
    <br>
    <br>
    Input 2 <input type="number" id="num2">
    <br>
    <br>
    Input 3 <input type="number" id="num3">
    <br>
    <br>
    Input 4 <input type="number" id="num4">
    <br>
    <br>
    Input 5 <input type="number" id="num5">
    <br>
    <br>
    <button onclick="calculate()">Calculate</button>

<h1 id="answer"></h1>
<h1 id="advice"></h1>

这是JS:

            function calculate()
        {
         var field1=document.getElementById("num1").value;
         var field2=document.getElementById("num2").value;
         var field3=document.getElementById("num3").value;
         var field4=document.getElementById("num4").value;
         var field5=document.getElementById("num5").value;

         if ( (field1>10) || (field1<1)|| (field2>10) || (field2<1) || (field3>10) || (field3<1) || (field4>10) || (field4<1) || (field5>10) || (field5<1))

         {
            alert("Enter a number between 1 and 10");
            return false;
        }

         var result=parseFloat(field1)+parseFloat(field2)+parseFloat(field3)+parseFloat(field4)+parseFloat(field5);

         var score= result / 5;




            if ((score==1) || (score==2) || (score==3))

         {

            document.getElementById("advice").innerHTML="You are between 1 and 3";

            }

            if ((score==4) || (score==5))

         {

            document.getElementById("advice").innerHTML="You are between 4 and 5";

            }

            if ((score==6) || (score==7))

         {

            document.getElementById("advice").innerHTML="You are between 6 and 7";

            }

            if ((score==8) || (score==9))

         {

            document.getElementById("advice").innerHTML="You are between 8 and 9";

            }

            if ((score==10))

         {

            document.getElementById("advice").innerHTML="You are 10";

            }

         if(!isNaN(score))

            {

            document.getElementById("answer").innerHTML="Your career score is " + score;

            }


        }

我们将非常感谢您的帮助!

2 个答案:

答案 0 :(得分:0)

if (score >= 1 && score < 3) {
  // do something
} else if (score >= 3 && score < 5) {
  // do something
}
 ... and so on

答案 1 :(得分:0)

我也想帮助您编写更好的代码:

此:

if ( (field1>10) || (field1<1)|| (field2>10) || (field2<1) || (field3>10) || (field3<1) || (field4>10) || (field4<1) || (field5>10) || (field5<1))
    {
        alert("Enter a number between 1 and 10");
        return false;
    }

更清晰的代码:

const fault = [field1, field2, field3, field4, field5].find(el => {
   if (el < 1 || el > 10) { // if we find this, then fault will be the index of this element
     return true 
   }  else { return false } // else fault will be undefined
})

if (fault) {
  alert("Enter a number between 1 and 10")
  return false
}