1

I am trying to write program that lists all numbers from 0-15 add whether the number is even odd (i.e. "0 is even, 1 is odd", etc.). I'm doing for my AP Computer Science Principles class and need to use a for loop in order to get full credit. I typed it like so:

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Loops</h2>

<p id="p"></p>

<script>
var text = "";
var i;


for (i = 0; i < 16; i++) {
    text += i + " is even" + "<br>";
}
document.getElementById("p").innerHTML = text;
</script>

</body>
</html>

but it outputs each integer value of i as even, like so:JS Loops Output How should I go about determining whether each integer value of I is even or odd? I tried using an else if statement in the for loop, but it made it not output anything.

M. Rodriguez
  • 11
  • 1
  • 3
  • 3
    Possible duplicate of [Testing whether a value is odd or even](https://stackoverflow.com/questions/6211613/testing-whether-a-value-is-odd-or-even) – Ivar Nov 07 '17 at 21:28
  • 1
    As @Ivar says in the comment above, your question is possibly a duplicate of that question. But for the sake of it, have a look on modulus operator and what it gives you. – Johan Brännmar Nov 07 '17 at 21:38

1 Answers1

-1
<script>
    let text = "";
    let i;

    for (i = 0; i < 16; i++) {
        if (i % 2 == 0) {
            text += i + " is even" + "<br>";
        } else {
            text += i + " is odd" + "<br>";
        }
    }
    document.getElementById("p").innerHTML = text;
</script>