JavaScript for loop
by teastwood
HTML
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript For Loop</h2>
<p>This is a function that will return the even numbers from an array</p>
<p>The result is:</p>
<div id="demo"></div>
</body>
</html>
CSS
body {
background-color: black;
color: white;
font-family: 'Helvetica', sans-serif;
}
#demo {
font-size: 24px;
}
JavaScript
const numArray = [1, 6, 9, 12, 18, 23, 47, 52];
let evenNums = [];
function findEvenNums(numArray) {
for (let i = 0; i < numArray.length; i++) {
if (numArray[i] % 2 === 0) {
evenNums.push(numArray[i]);
}
}
return evenNums;
}
//display the result in the console
console.log(findEvenNums(numArray));
//display result in the HTML div
document.getElementById("demo").innerHTML = evenNums;