我需要在数组中推送新对象。每个对象都包含属性(名称,sName,年龄,职业和show方法,其中显示所有用户信息)。数组正在由用户填充。 (提示) 但我有确认的问题。当我按下“取消”按钮时,它仍然继续工作。这是我的代码。
var staff = [];
var askAgain = true;
while(askAgain==true) {
var employee = {
name: prompt("enter the name of the employee"),
sName: prompt("enter the sName of the employee"),
age: prompt("enter the age of the employee"),
occupation: prompt("enter the occupation of the employee"),
show: function(){
document.write(' employee: ' + staff[1].name + ' ' + staff[1].sName + ', ' + staff[1].age + ', ' + staff[1].occupation + ' <br> ' );} }
staff.push(employee);
console.log(staff);
window.confirm( "Would you like to go again?" );
if (confirm == true){
askAgain == true;}
else {
askAgain==false;
}
}
答案 0 :(得分:5)
assignment =
需要一个等号。
if (window.confirm( "Would you like to go again?")) {
askAgain = true;
} else {
askAgain = false;
}
您可以仅分配window.confirm
的值。
askAgain = window.confirm( "Would you like to go again?");
当您开始收集至少一件物品时,您可以将while
支票移到底部并直接使用确认而无需任何变量。
var staff = [],
employee;
do {
employee = {
name: prompt("enter the name of the employee"),
sName: prompt("enter the sName of the employee"),
age: prompt("enter the age of the employee"),
occupation: prompt("enter the occupation of the employee"),
};
staff.push(employee);
} while (window.confirm("Would you like to go again?"))
console.log(staff);