我正在开发一个记录客户名称和状态(儿童/成人)的程序,该程序允许从阵列中添加,显示和删除客户记录。但是,如果用户输入相同的名称和状态,例如:名称:詹姆斯,状态:成人名称:詹姆斯,状态:成人我希望函数仅删除一条记录,但是现在它删除了两条记录,我是否必须在此处添加中断?请帮忙。PS:我不能使用任何内置的JavaScript函数,例如slice(),delete(),concat(),join(),pop(),push(),reverse(),shift(),slice(),,sort(),splice(),toString()或unshift()const MAX_CUSTOMERS = 5; //create new Array var customerList = new Array(); function addCustomer() //add customer { if (customerList.length >= MAX_CUSTOMERS) //check max customers alert('Sorry, no more than ' + String(MAX_CUSTOMERS) + ' customers are allowed on the trampoline.') else { var newIndex = customerList.length; //add new user customerList[newIndex] = new Object; customerList[newIndex].name = prompt('What is the customer\'s name?'); //ask user enter their name customerList[newIndex].status = prompt('Are you a Child or an Adult?'); //ask user enter their status while (!(customerList[newIndex].status == 'child' || customerList[newIndex].status == 'adult')) //check user is child or adult { customerList[newIndex].status = (prompt('Error! Please Enter \'child\' or \'adult\':')); } } } function displayAllCustomers() //display customers { var message = ''; //create message for (var i = 0; i < customerList.length; i++) //loop customers { message += 'Name:' + customerList[i].name + ', Status: ' + String(customerList[i].status) + '. \n'; //add customer to message } if (message == '') //check message message = 'Sorry, there are no customer to display!'; alert(message); //output message }function identifyThenDeleteCustomer() //identify then delete customer { var customerName = prompt('Enter the name of the customer to delete:'); //get customer name var customerStatus = prompt('Enter \'child\' or \'adult\':'); //get customer status while (!(customerStatus == 'child' || customerStatus == 'adult')) //check customer status customerStatus = prompt('Error - enter \'child\' or \'adult\':'); deleteCustomer(customerName, customerStatus); //delete customer }function deleteCustomer(aName, aStatus) //delete customer { var newCustomerList = new Array(); //create new array for (var i = 0; i < customerList.length; i++) //loop customers { var customer = customerList[i]; if ((customer.name != aName) || (customer.status != aStatus)) //check customer { var newIndex = newCustomerList.length; //add new user newCustomerList[newIndex] = customer; } } if (newCustomerList.length < customerList.length) //check deleted { alert('The customer has been deleted.'); } else { alert('There are no customer to delete!'); } customerList = newCustomerList; //update customer list }<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"><head><meta charset="utf-8" /><title>Coursework 2</title><script src="ZouYuncongINSTG018cw2.js" type="text/javascript"></script></head><body><div><button type="button" onclick="addCustomer();">Add Customer</button><br><button type="button" onclick="displayAllCustomers();">Display All Customers</button><br><button type="button" onclick="identifyThenDeleteCustomer();">Identify then Delete Customer</button></div></body></html> (adsbygoogle = window.adsbygoogle || []).push({}); 最佳答案 您可以像这样设置删除功能,function deleteCustomer(aName, aStatus) //delete customer{ for (var i = 0; i < customerList.length; i++) //loop customers { var customer = customerList[i]; if ((customer.name == aName) && (customer.status == aStatus)) //check customer { customerList = array.splice(i, 1);//delete from array itself alert('The customer has been deleted.'); return;//stop } } alert('There are no customer to delete!');}它将仅删除一个。如您所说,您不能使用内置函数。在这种情况下,您必须在要删除的元素之前和之后复制元素。您可以拥有一个控制变量,标记您已经找到要删除的变量。因此不会再有删除操作。例如,function deleteCustomer(aName, aStatus) //delete customer{ var onedeleted = false; var newCustomerList = new Array(); //create new array for (var i = 0; i < customerList.length; i++) //loop customers { var customer = customerList[i]; if ((customer.name != aName) || (customer.status != aStatus) || onedeleted) //check customer { var newIndex = newCustomerList.length; //add new user newCustomerList[newIndex] = customer; } else onedeleted = true; } if (newCustomerList.length < customerList.length) //check deleted { alert('The customer has been deleted.'); } else { alert('There are no customer to delete!'); } customerList = newCustomerList; //update customer list} (adsbygoogle = window.adsbygoogle || []).push({});
08-16 03:49