-4

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>
  • 3
    The documentation is right there, ready to be Googled: [`continue`](//developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/continue). – Sebastian Simon Oct 14 '21 at 01:48
  • 1
    When ``k === 7`` or ``7 === 7`` your code says to SKIP it. Thus why it is being skipped and not printed. Read documentation. It's even written in your HTML: "loop will skip the iteration where k = 7." – Dexterians Oct 14 '21 at 02:20
  • Tha la dexterian. I am a beginner. Hence couldnt understand this simple trick..I thought if k ===7 continue. We have continue there. – Kalyan Sundar Oct 14 '21 at 02:23
  • `continue` means stop the iteration here and start the next one, it does *not* mean "hey keep going, all is well" (this would be pointless). This might be confusing but it is common terminology in many languages, and it is an important control flow to understand. In other languages like Ruby, they use `next` instead. – eugenhu Oct 14 '21 at 03:03
  • Thank you all. That helped me a lot. the more I question the better I get in programming. Cheers – Kalyan Sundar Oct 14 '21 at 03:48

3 Answers3

0

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>
Spectric
  • 30,714
  • 6
  • 20
  • 43
  • Thanks Spectric, But what is === and why its ignoring 7 evn though the value of K is 7 .. any idea? both are integer data types right – Kalyan Sundar Oct 14 '21 at 01:48
  • @KalyanSundar It's not ignoring 7. That's why 7 isn't being printed – Spectric Oct 14 '21 at 01:49
  • @Kalyan See [What does this symbol mean in JavaScript?](/q/9549780/4642212) and the documentation on MDN about [expressions and operators](//developer.mozilla.org/docs/Web/JavaScript/Reference/Operators) and [statements](//developer.mozilla.org/docs/Web/JavaScript/Reference/Statements). [`===`](//developer.mozilla.org/docs/Web/JavaScript/Reference/Operators/Strict_equality), [`if`](//developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/if...else), [What is the difference between the `=` and `==` operators and what is `===`? (Single, double, and triple equals)](/q/11871616/4642212). – Sebastian Simon Oct 14 '21 at 01:51
0

Just do a simple condition.

if (k != 7) {
   text += "The number is " + k + "<br>";
}
  • Thanks julcarl. I got it. But my question is why the value of k is not equal to 7. In that look the k will get assigned with number 7. So ideally 7 should be === 7 am I right. Why k as a variable with number 7 isnt equal to the daya type of 7 on the other side.. I didnt get answer to that query on google. – Kalyan Sundar Oct 14 '21 at 02:01
  • ahhh ok I get it now, I think the real question here is what is the difference between an integer and a variable? Is that correct? – Julcarl Selma Oct 14 '21 at 02:11
  • 1
    @KalyanSundar - When ``k === 7`` or ``7 === 7`` your code says to SKIP it. Thus why it is being skipped and not printed. Read documentation. It's even written in your HTML: "loop will skip the iteration where k = 7." – Dexterians Oct 14 '21 at 02:21
0

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.