how to find even numbers?

I am trying to find even numbers from 0 to 100 but it is not working. even it not showing any error. can anyone help me to figure out?

<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>

    <script>        
var x = 1;
        while (x <= 100) {
            if (x % 2 == 0) {
                console.log(x);
            } else {
                break;
            }
            x++;
        }
    </script>
</body>

</html>

22 thoughts on “how to find even numbers?”

  1. <html lang="en">
    
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Document</title>
    </head>
    
    <body>
    
        <script>
        var x = 1;
            while (x <= 100) {
                if (x % 2 == 0) {
                    console.log(x);
                } else {
                    break;
                }
                x++;
            }
        </script>
    </body>
    
    </html>
    

    It stops after the firs round, because "if (x % 2 == 0)" does not match and else is a "break"

    Reply
  2. for(let x =0; x<=100; x+=2){
    console.log(x);
    }

    I wouldn’t check for even numbers. It’s easier to just start by 0 and add 2 each iteration.

    Reply
  3. Here as a one liner:

    Array(50).fill().map((_, i) => (i+1)*2).forEach((v) => console.log(v))
    
    // Alternative
    console.log(Array(50).fill().map((_, i) => (i+1)*2))

    jk

    Reply

Leave a Comment