TOC
wwworkshop
Uni Köln

3.5
Loops and conditions

for can be used to execute some code multiple times. if can be used to execute some code in certain cicumstances.

???

The snippet shows every even number lower than ten by iterating through every number from 0-9 and checking if the number is divisable by two without a remainder using the modulo (%) operand.

It then creates a paragraph element for every such number, assigns the number as the text and appends the element to the document.

script.js

for (let i = 0; i < 10; i++) {
    if (i % 2 === 0) {
        const paragraph = document.createElement('p')
        paragraph.innerText = i
        document.body.appendChild(paragraph)
    }
}