将新的逗号分隔的数字字符串推送到现有数组 [英] Pushing new comma separated string of numbers to an existing array

查看:53
本文介绍了将新的逗号分隔的数字字符串推送到现有数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为numbers的现有数组,并通过onclick窗口提示事件添加了新值.问题是,据我了解,我正在推字符串,而不是数字...尽管可以.如何确保我添加数字而不是字符串?

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>

推荐答案

您是正确的,如果使用 prompt ,则输入的值是字符串.现在,您还添加了按逗号分隔的选项,因此您将需要使用

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>

这篇关于将新的逗号分隔的数字字符串推送到现有数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆