0

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>
Lucas
  • 5
  • 5
  • Your preferred search provider -> _"javascript convert string to number"_ (or ask the author of the script how to do it) – Andreas Feb 23 '20 at 18:51

1 Answers1

1

You are correct, if you use prompt the value entered is a string. Now you also added the option to split by comma so you will need to use parseInt() to parse the numbers to an integer. You can do like so:

//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>`;
        console.log(numbers);
    }
    document.getElementById("result").innerHTML = `...${max} <br>`;

}

printNumbers();

//Function onclick Add More numbers
function addMoreNumbers() {
    let moreNumbers = window.prompt("Add comma separated numbers");
    newNumbersArray = moreNumbers.split(',');
    newNumbersArray.forEach(function(newNumber){
        numbers.push(parseInt(newNumber, 10));
    });  
   printNumbers();
}
<h3 id="title"></h3>
<div id="numbers"></div>
<h3 id="result"></h3>
<button onclick="addMoreNumbers()">Add comma separated numbers</button>
caramba
  • 21,963
  • 19
  • 86
  • 127
  • Sorry Andreas, just a newbie... thanks for the answer Caramba. I did find a solution also here: https://stackoverflow.com/questions/15677869/how-to-convert-a-string-of-numbers-to-an-array-of-numbers – Lucas Feb 23 '20 at 19:12