In: Computer Science
<!doctype html>
<html lang="en">
<head>
<title>Programming 1</title>
<meta charset="utf-8"/>
</head>
<body>
<script type="text/javascript">
function findAnswer() {
var a = document.getElementById("box1").value;
var b = document.getElementById("box2").value;
if (typeof(a) == "number" && typeof(b) == "number") {
var c = (a + b) % 5;
document.getElementById("ans").innerHTML = c.toString();
} else {
document.getElementById("ans").innerHTML = "no Good";
}
}
</script>
<form action="#" method="post" name="form1" id="form1" class="form" >
<label>Enter Numbers: </label> <br />
<label> Value 1 : </label><input type="text" name="box1" id="box1" value="10" /><br/>
<label> Value 2 : </label><input type="text" name="box2" id="box2" value="20" /><br/>
<br/> <br />
<input type="button" name="Submit" id="submit" value="Submit" onclick="findAnswer();"/>
</form>
<p id ="ans"></p>
</body>
</html>
Will the code from above print anything, if so what printed ? If not, say NONE.
HTML Code:
<!doctype html>
<html lang="en">
<head>
<title>Programming 1</title>
<meta charset="utf-8"/>
</head>
<body>
<script type="text/javascript">
function findAnswer() {
var a = parseInt(document.getElementById("box1").value);
var b = parseInt(document.getElementById("box2").value);
if (typeof(a) == "number" && typeof(b) == "number") {
var c = (a + b) % 5;
document.getElementById("ans").innerHTML = c.toString();
} else {
document.getElementById("ans").innerHTML = "NaN";
}
}
</script>
<form action="#" method="post" name="form1" id="form1" class="form" >
<label>Enter Numbers: </label> <br />
<label> Value 1 : </label><input type="text" name="box1" id="box1" /><br/>
<label> Value 2 : </label><input type="text" name="box2" id="box2" /><br/>
<br/> <br />
<input type="button" name="Submit" id="submit" value="Submit" onclick="findAnswer();"/>
</form>
<p id ="ans"></p>
</body>
</html>
Output 1:
It first add two input values, and then calculate the module by 5. Finally in result, it prints the remainder value.
Here => 5+8 => 13 % 5 => 3
Output 2:
If one of the input values not a number, then it simply prints the output NaN => Not A Number
Thumbs Up Please !!!