Javascript - 如何将提示输入保存到数组中 [英] Javascript - How to save prompt input into array
问题描述
我在使用Javascript时遇到了一些问题。
几周前我们刚刚开始研究它,我必须在课堂上做一些工作:
I`m having some issue with Javascript. We just started to study it a couple weeks ago and I have to do a work for class:
需要做一个提示。从用户那里获得10个数字输入(10个等级)。
将数字放入一个数组然后用它做一些函数。
Need to do a prompt. get 10 numbers input (10 grades) from the user. put the numbers into an array and then do some functions with it.
我的问题是如何在数组中保存输入?
我们已经了解了所有的循环。试图在网上搜索,但没有找到答案。
My question is how do I save the input in the array? We learned about all the loops already. Tried to search online but didn`t found an answer.
如果有人能解释我是怎么做的,我很乐意。
非常感谢。
I would love if somebody could explain how I do it. Thank you very much.
推荐答案
试着让他们输入他们的数字或成绩,用逗号分隔然后你可以拆分它。
Just try to ask them to input their numbers or grade, separated by a comma and then you can split on it.
var arr = prompt("Enter your numbers").split(",")
或者,要求提示
十次
var arr = [];
for(var i = 0; i < 10; i++)
arr.push(prompt("Enter a number");
如果您希望它们成为数字,只需在前缀
前缀 +
,所以它变成一个数字(假设它们是实际数字)或只是
If you want them to be numbers, just prefix prompt
with +
, so it becomes a number(provided they're actual numbers) or just do
arr = arr.map(Number);
这篇关于Javascript - 如何将提示输入保存到数组中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!