Built JS code that gave even numbers but Now what if I were to give you a range (a start at and stop at) of numbers and you print only the even numbers again? Create a function that would be able to handle any range when given a start and stop. All ranges will be positive in nature. Can some one help me? I’ve got this so far,
function printEvenNumbers() {
for (let i = 0; i <= 100; i += 2) {
console.log(i);
}
}
printEvenNumbers();
To give you a way forward, if you divide a number by two and the remainder of that division is zero, the number you divided was even, if the remainder is not zero then the number you divided was odd. There is a JavaScript arithmetic operator %
which you can use to perform a division and return the remainder of the division. e.g.
4 % 2 = 0
5 % 2 = 1
i.e. four divided by two equals two with a remainder of zero
& five divided by two equals two with a remainder of one
Hope that helps a little.