lundi 17 décembre 2018

Javascript conditional statements with range input values

could someone help me with the following Javascript code. I want the conditional statements to execute if the value of "score" is between the ranges of 1-3, 3-5, 5-7, 7-9 and 9-10.

Currently, they only execute if the variable "score" is an integer, but I want the scores to be executed between the range of numbers.

for example, I want the line "if ((score==1) || (score==2) || (score==3))" to be something like if( score is between 1 and 3), Then the statement will be executed.

This is the 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>

This is the 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;

            }


        }

Help will be super appreciated!

Aucun commentaire:

Enregistrer un commentaire