What are Even Numbers? | Maths Explanation for JavaScript Kids
Hello pupils, what are even numbers?
Even numbers are numbers that are divisible by 2.
They include:
Considering how simple and straight-forward even numbers are, writing a JavaScript code for even numbers
serves as a great way to introduce our Mathematics educational activities for young learners.
Well then, let's see how we can write a programming code to make our computer
list a set of even numbers in the JavaScript language, between a given range of values.
Bear in mind, we use only a loop and a simple conditional statement for the JavaScript code.
Type out the adjoining JavaScript code for listing even numbers.
Creating HTML Files
Create a new file; On Notepad++: File, New.Call it EvenNumbers.html.
Remember to select the right folder (usingMaths) if necessary and change Type to All.
So! JavaScript Fun Practice Exercise - List Even Numbers
As a fun practice exercise, feel free to try out your own boundary values, and see how the JavaScript code lists the even numbers between those boundary values.
JavaScript Code for Even Numbers
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>List Even Numbers</title>
</head>
<body>
<h3>Here is a List of Even Numbers Between 2 and 100 Inclusive</h3>
<!-- This is where the result will be displayed when it is ready.-->
<div id="even_numbers"></div>
<script>
var start = 2; // This variable holds the first of our even numbers.
var end = 100; // This variable holds the last number for our series.
/* Loop through and collect all the even numbers. */
while (start <= 100) {
if ((start % 2) == 0) {
document.getElementById("even_numbers").innerHTML += start + ", "; // Mind the '+' before the '=' sign.
}
start = start + 1;
}
</script>
</body>
</html>