I have an a existing array called numbers and via an onclick window prompt event I add new values. The problem is that as far I understand I'm pushing strings, not numbers... although it works. How can I assure I add numbers instead of strings?
<!DOCTYPE html>
<html lang="es">
<head>
<meta charset="UTF-8">
<title>Adding comma separated values to an existing array</title>
<script>
//Create the array
let numbers = [20, 15, 25, 49, 25, 50, 35, 60, 40];
//Print the whole thing
function printNumbers() {
let printedNumbers = numbers.toString();
document.getElementById("title").innerHTML = `The highest number of the lis: ${printedNumbers} is...`;
let max = numbers[0];
for (let i = 0; i <= (numbers.length - 1); i++) {
if (numbers[i] > max) {
max = numbers[i];
}
document.getElementById("numbers").innerHTML += numbers[i] + ` | MAX: ${max} <br>`;
}
document.getElementById("result").innerHTML = `...${max} <br>`;
console.log(numbers);
}
//Function onclick Add More numbers
function addMoreNumbers() {
let moreNumbers = window.prompt("Add comma separated numbers");
arr = moreNumbers.split(',');
Array.prototype.push.apply(numbers, arr);
printNumbers();
}
</script>
</head>
<body onload="printNumbers();">
<h3 id="title"></h3>
<div id="numbers"></div>
<h3 id="result"></h3>
<button onclick="addMoreNumbers()">Add comma separated numbers</button>
</body>
</html>