问题
如何仅使用一个 prompt();
将多个值添加到列表中?
代码
let items = [];
action = prompt("Enter");
如果我的输入是 Hello World!
,那么我怎样才能使我的列表看起来像这样:
items = ["Hello", "World!"];
尝试
这是我能得到的最接近的结果(它失败了,因为我只能使用一个 prompt();
):
let first = prompt("Enter 1");
let second = prompt("Enter 2");
items.push(first);
items.push(second);
最佳答案
您可以拆分接收到的字符串以获得具有两个单独值的数组。
let action = prompt();
let items = action.split(' ');
console.log(items);
关于javascript - 将 'prompt()' 的结果拆分为数组中的项目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47253565/