Home > Blockchain >  why it is ignoring the number 7?
why it is ignoring the number 7?

Time:10-14

This code is supposed to skip the number 7 because there is a declaration of var using === operator. My question is what do we need to make it include number 7. Is it because it has been declared as variable and hence its ignoring it. 7 can be a variable right? or is it an integer value ?

<!DOCTYPE html>
<html>
    <body>
          
<p>A loop with a <mark>continue</mark> statement.</p>
  
  
          
<p>loop will skip the iteration where k = 7.</p>
  
  
        <p id="maddy"></p>
  
  
        <script>
            var text = "";
            var k;
            for (k = 0; k < 10; k  ) {
                if (k === 7) {
                    continue;
                }
                text  = "The number is "   k   "<br>";
            }
            document.getElementById("maddy").innerHTML = 
              text;
        </script>
    </body>
</html>

CodePudding user response:

Just remove the if statement that checks whether k is 7?

<!DOCTYPE html>
<html>
    <body>
          
<p>A loop with a <mark>continue</mark> statement.</p>
  
  
          
<p>loop will skip the iteration where k = 7.</p>
  
  
        <p id="maddy"></p>
  
  
        <script>
            var text = "";
            var k;
            for (k = 0; k < 10; k  ) {
                text  = "The number is "   k   "<br>";
            }
            document.getElementById("maddy").innerHTML = 
              text;
        </script>
    </body>
</html>

CodePudding user response:

Just do a simple condition.

if (k != 7) {
   text  = "The number is "   k   "<br>";
}

CodePudding user response:

Have a look at your logic. It is running a while loop from 1 to 10. There you are checking whether the number is 7 in the if condition and if that is true you are telling the loop to finish that iteration from that point and not to continue. So the code never reaches[document.getElementById("maddy").innerHTML = text;]. So it is not displayed.

  • Related