在JavaScript中将用户输入的元素输入到数组中
Entering user inputed elements into an array in JavaScript
我想知道我的代码到底丢失了什么,或者为什么它不能工作
<script>
var index = 0;
var name = [];
while (true)
{
var add = window.confirm("Would you like to enter an employee");
if (add === true)
{
name[index] = prompt("Please enter and employee name");
++index;
}
else
break;
}
window.alert(name[0]);
</script>
窗口。alert只是看它是否被保存,我尝试在JS中使用c++风格,我知道我可能应该使用push方法?但是我不是很确定,所有在警告框中说的都是未定义的,我也不是很确定为什么,任何帮助都会非常感激,谢谢。我不确定在javascript中什么是允许的,什么是不允许的,我假设像递增这样的事情在JS和c++中是相同的
你需要有一个数组和push元素,
var index = 0;
var names = [];
while (true) {
var add = window.confirm("Would you like to enter an employee");
if (add === true) {
var newname = prompt("Please enter and employee name");
names.push(newname);
++index;
} else
break;
}
window.alert(names);
使用push
向数组中添加新的输入,如下所示:
var index = 0, employee;
var nameArr = [];
while (true)
{
var add = window.confirm("Would you like to enter an employee");
if (add === true)
{
employee = prompt("Please enter and employee name");
nameArr.push(employee);
++index;
}
else
break;
}
window.alert(nameArr[0]);
console.log(nameArr);
一点递归
function addEmployee(arr) {
let name = arr ? arr : [];
let yes = confirm("Would you like to enter an employee");
if (yes) {
let employee = prompt("Please enter and employee name");
name.push(employee);
addEmployee(name);
} else {
alert(name);
}
};
addEmployee();
相关文章:
- $q.all当输入数组中的一项不是promise时,Typescript检查器失败
- 你能提交一个带有文本输入数组的表单吗
- 使用 jQuery 和 Ajax 发送输入数组
- $.when.apply 根据输入数组的长度返回具有不同结构的结果
- PHP只读取动态创建的输入数组的第一个值
- 使用jquery设置输入数组值
- 使用 javascript 验证输入数组
- 角度 UI 预测文本:如何输入未包含在输入数组中的数据
- 将动态输入索引到其他动态输入数组
- 循环遍历输入数组以设置值,“无法设置未定义的属性值”
- 在输入数组元素时在 html 页面中显示数组元素
- Javascript 隐藏输入数组
- 编码然后在 base64 中解码不会输出我的输入数组缓冲区
- 将隐藏的输入数组传递给 jQuery load()
- 将输入数组中的特定输入值发送到 JavaScript
- 在输入数组的 split() 和子字符串 () 之后的文本区域中显示
- Jquery 添加未传递给 php 的输入数组字段
- Javascript函数显示用户输入数组
- 从输入数组中获取sum's元素
- 如何在文本输入数组中插入字符串