https://jsfiddle.net/gcqojLfp/2/
1.) The js fiddle isn't rendering the <div>
with the id result2
from the HTML.
After clicking the button, the multiply()
function should run and it should get the innerHTML
of the <div>
with id result2
.
I'm not sure why nothing is happening after clicking on the button in the fiddle. It should say:
"All of the numbers between 1 and 5 multiplied together equals: 120"
(if 5 is entered, for example). We should see this displayed in the result2
div.
2.) Why is the text field with id factorialInput
not restricting the input to 10 at the max? I thought that I had set the max for this input field here:
<input id="factorialInput" type="number" max="10" size="20"/>
(as per answers from other articles).
I've tried maxlength
as well - I don't understand why it's allowing numbers 11 and up.
Any ideas?
JavaScript
function multiply() {
let num = document.getElementById("factorialInput").value;
let sum = 1;
for (let i=1; i<= num; i++) {
sum = sum * i;
}
document.getElementById("result2").innerHTML = "All of the numbers between
1 and " + num + " multiplied together equals: " + sum ;
}
HTML
<div class="row">
<form id="enterValue">
Enter a number between 1 and 10:
<input id="factorialInput" type="number" max="10" size="20"/>
<input type ="button" value="Submit" onclick="multiply();" max="10" />
</form>
<br><br>
<div id="result2"></div>
</div>
CSS
.row {
height: 200px;
}
Thanks!