使用 Javascript 将数组中的任意数量的项目加在一起
Adding together any number of items in an array with Javascript
我正在学习 Javascript,目前在创建应用程序时遇到问题。我想创建一个网页,它将获取在文本框中输入的值,并将它们放在一个数组中。然后,我想创建一个将这些值相加的函数。我几乎完成了,但我很难弄清楚如何创建一个将数组项加在一起的函数。我最大的问题是可以在页面中输入任意数量的值,而我能找到的所有文档都是基于预先设置的数组。这是我的代码:
HTML:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Numbers</title>
</head>
<body>
<section>
<header class="header m-2" id="myForm">Numbers App</header>
<section class="row m-2">
<label class="inputLabel">Number: <input type="number" id="numInput"></label>
</section>
<button class="button m-1" onclick="displayText(); addNum(); testCount();" id="addButton">Add Number</button>
<button class="button m-1" disabled>Calculate</button>
<button class="button m-1" onclick="resetPage();">Reset</button>
</section>
<section id="numForm">
<div class="numLabel m-2" id="numLabel">Numbers Added: </div>
<p class="m-2 mt-3" id="numValue"></p>
</section>
<script src="script.js"></script>
</body>
</html>
JS:
//Display "Numbers Added: "
function displayText() {
document.getElementById("numLabel").style.display = "block";
}
//Add the entered values into the array
let numArray = [];
function addNum() {
let num = document.getElementById("numInput").value;
numArray.push(num);
document.getElementById("numValue").innerHTML = numArray.join(" ");
}
//Testing count function
function testCount() {
for(count = 0; count > 2; count++) {
console.log("this works");
}
}
//Reset the page
function resetPage() {
//Clear input field
document.getElementById("numInput").value = "";
//Hide "Numbers Added: "
document.getElementById("numLabel").style.display = "none";
//Clear array items
numArray = [];
document.getElementById("numValue").innerHTML = "";
}
编辑:
要显示加法可以只使用类似的东西:
const array1 = [1, 2, 3, 4];
const result = array1.reduce((acc, curr) => parseInt(curr) + parseInt(acc));
let additionDisp = array1.join(" + ") + " = " + result;
console.log(additionDisp);
您需要先声明您的add
函数,parse
您的字符串输入为整数值并执行归约以获得总和
//Add the entered values into the array
let numArray = [];
//Display "Numbers Added: "
function displayText() {
var result = numArray.reduce((acc, curr) => parseInt(curr) + parseInt(acc), 0);
var numSumString = "";
for (data of numArray) {
numSumString = data + " + " + numSumString;
}
document.getElementById("numLabel").innerHTML = "Numbers Added:" + numSumString.substring(0, numSumString.length - 2) + "=" + result;
}
function addNum() {
let num = document.getElementById("numInput").value;
numArray.push(num);
document.getElementById("numValue").innerHTML = numArray.join(" ");
}
//Reset the page
function resetPage() {
//Clear input field
document.getElementById("numInput").value = "";
//Hide "Numbers Added: "
document.getElementById("numLabel").style.display = "none";
//Clear array items
numArray = [];
document.getElementById("numValue").innerHTML = "";
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Numbers</title>
</head>
<body>
<section>
<header class="header m-2" id="myForm">Numbers App</header>
<section class="row m-2">
<label class="inputLabel">Number: <input type="number" id="numInput"></label>
</section>
<button class="button m-1" onclick="addNum();" id="addButton">Add Number</button>
<button class="button m-1" onclick="displayText();">Calculate</button>
<button class="button m-1" onclick="resetPage();">Reset</button>
</section>
<section id="numForm">
<div class="numLabel m-2" id="numLabel">Numbers Added: </div>
<p class="m-2 mt-3" id="numValue"></p>
</section>
<script src="script.js"></script>
</body>
</html>
const array = [0, 42, 101, 5, 2];
const total = array.reduce((sum, x) => sum + x);
console.log(total);
我正在学习 Javascript,目前在创建应用程序时遇到问题。我想创建一个网页,它将获取在文本框中输入的值,并将它们放在一个数组中。然后,我想创建一个将这些值相加的函数。我几乎完成了,但我很难弄清楚如何创建一个将数组项加在一起的函数。我最大的问题是可以在页面中输入任意数量的值,而我能找到的所有文档都是基于预先设置的数组。这是我的代码:
HTML:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Numbers</title>
</head>
<body>
<section>
<header class="header m-2" id="myForm">Numbers App</header>
<section class="row m-2">
<label class="inputLabel">Number: <input type="number" id="numInput"></label>
</section>
<button class="button m-1" onclick="displayText(); addNum(); testCount();" id="addButton">Add Number</button>
<button class="button m-1" disabled>Calculate</button>
<button class="button m-1" onclick="resetPage();">Reset</button>
</section>
<section id="numForm">
<div class="numLabel m-2" id="numLabel">Numbers Added: </div>
<p class="m-2 mt-3" id="numValue"></p>
</section>
<script src="script.js"></script>
</body>
</html>
JS:
//Display "Numbers Added: "
function displayText() {
document.getElementById("numLabel").style.display = "block";
}
//Add the entered values into the array
let numArray = [];
function addNum() {
let num = document.getElementById("numInput").value;
numArray.push(num);
document.getElementById("numValue").innerHTML = numArray.join(" ");
}
//Testing count function
function testCount() {
for(count = 0; count > 2; count++) {
console.log("this works");
}
}
//Reset the page
function resetPage() {
//Clear input field
document.getElementById("numInput").value = "";
//Hide "Numbers Added: "
document.getElementById("numLabel").style.display = "none";
//Clear array items
numArray = [];
document.getElementById("numValue").innerHTML = "";
}
编辑: 要显示加法可以只使用类似的东西:
const array1 = [1, 2, 3, 4];
const result = array1.reduce((acc, curr) => parseInt(curr) + parseInt(acc));
let additionDisp = array1.join(" + ") + " = " + result;
console.log(additionDisp);
您需要先声明您的add
函数,parse
您的字符串输入为整数值并执行归约以获得总和
//Add the entered values into the array
let numArray = [];
//Display "Numbers Added: "
function displayText() {
var result = numArray.reduce((acc, curr) => parseInt(curr) + parseInt(acc), 0);
var numSumString = "";
for (data of numArray) {
numSumString = data + " + " + numSumString;
}
document.getElementById("numLabel").innerHTML = "Numbers Added:" + numSumString.substring(0, numSumString.length - 2) + "=" + result;
}
function addNum() {
let num = document.getElementById("numInput").value;
numArray.push(num);
document.getElementById("numValue").innerHTML = numArray.join(" ");
}
//Reset the page
function resetPage() {
//Clear input field
document.getElementById("numInput").value = "";
//Hide "Numbers Added: "
document.getElementById("numLabel").style.display = "none";
//Clear array items
numArray = [];
document.getElementById("numValue").innerHTML = "";
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Numbers</title>
</head>
<body>
<section>
<header class="header m-2" id="myForm">Numbers App</header>
<section class="row m-2">
<label class="inputLabel">Number: <input type="number" id="numInput"></label>
</section>
<button class="button m-1" onclick="addNum();" id="addButton">Add Number</button>
<button class="button m-1" onclick="displayText();">Calculate</button>
<button class="button m-1" onclick="resetPage();">Reset</button>
</section>
<section id="numForm">
<div class="numLabel m-2" id="numLabel">Numbers Added: </div>
<p class="m-2 mt-3" id="numValue"></p>
</section>
<script src="script.js"></script>
</body>
</html>
const array = [0, 42, 101, 5, 2];
const total = array.reduce((sum, x) => sum + x);
console.log(total);