I'm working on an HTML coding exercise. The HTML file will let the users input a starting number, an ending number, and a step number. Then output all the even numbers between the starting number and the ending number by the step size after clicking a button. But so far nothing displays after the button is clicked. Also, I tried to insert text into the paragraph using span tags but that doesn't work. Please help. Thank you.
function validateItems(){
var starting = document.forms["displayEvens"]["starting"].value;
var ending = document.forms["displayEvens"]["ending"].value;
var step = document.forms["displayEvens"]["step"].value;
var numbers = "";
var adding = starting;
if (starting == "" || isNaN(starting)){
alert("Starting Number must be filled with a number");
document.forms["displayEvens"]["starting"].focus();
return false;
}
if (ending == "" || isNaN(ending) || ending <= starting){
alert("Ending Number must be filled with a number or must be greater than the starting number");
document.forms["displayEvens"]["ending"].focus();
return false;
}
if (step == "" || isNaN(step) || step < 0){
alert("Step Number must be filled with a number and must be positive");
document.forms["displayEvens"]["step"].focus();
return false;
}
while (adding < ending){
adding = adding + step;
if (adding % 2 == 0){
numbers = String(adding) + " ";
}
}
document.getElementById("first").innerText = starting;
document.getElementById("second").innerText = second;
document.getElementById("middle").innerText = step;
document.getElementById("numbers").innerText = numbers;
return false;
}
<!DOCTYPE html>
<html>
<head>
<script type="text/JavaScript" src="displayEvens.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"
rel="stylesheet" type="text/css" />
</head>
<body>
<div class="container">
<h1>Display Evens</h1>
<form name="displayEvens" onsubmit="return validateItems();">
<div class="form-group">
<label for="starting">Starting Number</label>
<input type="number" class="form-control" id="starting">
</div>
<div class="form-group">
<label for="ending">Ending Number</label>
<input type="number" class="form-control" id="ending">
</div>
<div class="form-group">
<label for="step">Step</label>
<input type="number" class="form-control" id="step">
</div>
<button type="submit" class="btn btn-default">Display Evens</button>
</form>
<p>Here are the even numbers between <span id="first"></span> and <span id="second"></span> by <span id="middle"></span>'s:</p><br />
<p><span id="results"></span></p>
</div>
</body>
</html>
id="numbers"to matchgetElementById("numbers"). Should that reference"results"instead?adding = adding + stepis a string concatenation andwhile (adding < ending)is a string comparison. And"100" < "5" === true.starting=2, ending= 6 and step=3. First iteration,2<6 and adding=5Second iteration,5<6 and adding=8and it appends 8 to the string since it's an even number. But 8 exceeds 6. Try this instead:adding = adding + step; while (adding < ending){ if (adding % 2 == 0){ numbers = numbers+adding + " "; } adding = adding + step; }isNaNdoes not determine wether some value itself is not a number, it is only determining wether something is explicitelyNaN(when cast to a number). soisNaN("5") === false,isNaN([ "0xF" ]) === falseevenisNaN({ foo: "lorem ipsum", toString(){ return "" }}) === falsebecause they all can be cast to a number that is notNaNvar starting =parseInt (document.forms["displayEvens"]["starting"].value); var ending = parseInt (document.forms["displayEvens"]["ending"].value); var step = parseInt (document.forms["displayEvens"]["step"].value);stackoverflow.com/questions/14496531/…