Core JavaScript

Using map() on an Array

This program demonstrates how to use the map function of an array. In this case, the map output an associated array that tells whether the corresponding number in the original array is even.

UsingAMap.html

<!DOCTYPE html>
<html>
	<head>
		<title>XoaX.net's Javascript</title>
		<script type="text/javascript" src="UsingAMap.js"></script>
    <style>
      th, td {
        text-align: center;
        border: 1px solid black;
        padding: 3px;
      }
      table {
        background-color:white;
        border: 1px solid black;
      }
    </style>
	</head>
	<body onload="fnInitialization()">
		<table>
			<tr id="idNumberRow">
			</tr>
			<tr id="idBoolRow">
			</tr>
		</table>
	</body>
</html>

UsingAMap.js

function fnInitialization() {
	const kiaNumbers = [4,6,7,9,2,4,5,3,5,2,1,8];
	const kbaAreEven = kiaNumbers.map(IsEven);

	// Output the original array of numbers into the table
	var qNumberRow = document.getElementById("idNumberRow");
	qNumberRow.innerHTML += "<th>Numbers</th>"
	for (var i = 0; i < kiaNumbers.length; ++i) {
		qNumberRow.innerHTML +="<td>"+kiaNumbers[i]+"</td>"
	}
	// Output the bool even check values into the second row of the table
	var qBoolRow = document.getElementById("idBoolRow");
	qBoolRow.innerHTML += "<th>Is Even</th>"
	for (var i = 0; i < kiaNumbers.length; ++i) {
		qBoolRow.innerHTML +="<td>"+kbaAreEven[i]+"</td>"
	}
}

function IsEven(dN) {
	return ((dN % 2) == 0);
}
 

Output

 
 

© 2007–2025 XoaX.net LLC. All rights reserved.