准备脚本以将数据存储到数组中。用户可以输入任何数字,但只能存储可被数字三整除的值。然后,将数据(存储在数组中)显示在 HTML 页面上。
我需要使用 JavaScript 代码来执行此操作。如何储值和显示?谢谢。
最佳答案
这是你需要的吗?
var numbers = []; // Array to store the valid numbers
function openPromt() {
var num = prompt("Please enter a number", "0");
storeIfNumValid(num);
}
openPromt();
// funtion that will call after each value from `prompt`
function storeIfNumValid(num) {
if (num != null && num % 3 === 0) {
numbers.push(num);
openPromt();
} else {
console.log(numbers);
document.getElementById("demo").innerHTML = numbers;
}
}
<div id="demo"></div>
关于javascript - 用户输入任意数字但只能存储可被 3 整除的数字并显示回来,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53468183/