I'm trying to write a function in JavaScript to complete this exercise. I've got a function written that will display the even numbers. However, I'm having a hard time figuring out how to get the function to display the even numbers by a step like in the example.
Allow a user to enter a starting number an ending number and a step value into three textboxes on a page. A button will be present that when clicked should output all the even numbers between the start and end value. illustration example of exercise
In the example above the user has entered 4 and 20 as the start and end, respectively and set the step to 3. This means the numbers 4, 7, 10, 13, 16, and 19 were considered, but only 4, 10, and 16 were displayed.
This is the code I've written so far:
function showEvens () {
var start = document.getElementById("start").value;
var end = document.getElementById("end").value;
var step = document.getElementById("step").value;
var evenNums = "<br>Even Numbers:<br>";
for(i=start; i<=end; i++) {
if (i%2 == 0) {
evenNums += i + "<br>";
}
}
document.getElementById("result").innerHTML = evenNums;
}