Javascript program - deleting an element from an array -



Javascript program - deleting an element from an array -

the programme wrote how sports equipment company monitor trampoline use; records client name, , status (child or adult) on trampoline. there 5 functions, can add together customer, display client , delete lastly customer. stuck on lastly function have utilize object constructor identify , delete customer.

ps: can't utilize predefined javascript array element-deleting or manipulating methods such delete(), concat(), join(), pop(), push()

class="lang-js prettyprint-override">const max_customers = 5; //maximum client on trampoline 5 var customerlist = new array();//create new array function addcustomer () { if (customerlist.length >= max_customers) alert('sorry, no more ' + string(max_customers) + ' customers allowed on trampoline.') else { var newindex = customerlist.length; customerlist[newindex] = new object; customerlist[newindex].name = prompt('what customer\'s name?'); //ask user come in name customerlist[newindex].status = prompt('are kid or adult?'); //ask user come in status while (!(customerlist[newindex].status == 'child' || customerlist[newindex].status == 'adult')) { customerlist[newindex].status = (prompt('error please come in \'child\' or \'adult\':')); } } } function displayallcustomers () { var message = ''; (var = 0 ; < customerlist.length ; i++) { message += customerlist[i].name + ', status: ' + string(customerlist[i].status) + '. \n'; } if (message == '') message = 'there no client display!'; alert(message); } function deletelastcustomer () { if (customerlist.length > 0) { customerlist.length--; alert('the lastly client has been deleted.'); } else alert('there no client delete!'); } function identifythendeletecustomer () { var customername = prompt('enter name of client delete:'); var customerstatus = prompt('enter \'child\' or \'adult\':'); while (!(customerstatus == 'child' || customerstatus == 'adult')) customerstatus = prompt('error - come in \'child\' or \'adult\':'); deletecustomer(customername,customerstatus); } function deletecustomer (aname, astatus) { ; }

solution

//maximum client on trampoline 5 const max_customers = 5; //create new array var customerlist = new array(); //add client function addcustomer() { //check max customers if (customerlist.length >= max_customers) { alert('sorry, no more ' + string(max_customers) + ' customers allowed on trampoline.'); } else { //add new user var newindex = customerlist.length; customerlist[newindex] = new object; //ask user come in name customerlist[newindex].name = prompt('what customer\'s name?'); //ask user come in status customerlist[newindex].status = prompt('are kid or adult?'); //check user kid or adult while (!(customerlist[newindex].status == 'child' || customerlist[newindex].status == 'adult')) { customerlist[newindex].status = ( prompt('error please come in \'child\' or \'adult\':')); } } } //display customers function displayallcustomers() { //create message var message = ''; //loop customers (var = 0; < customerlist.length; i++) { //add client message message += customerlist[i].name + ', status: ' + string(customerlist[i].status) + '. \n'; } //check message if (message == '') { message = 'there no client display!'; } //output message alert(message); } //delete lastly client function deletelastcustomer() { //check client list if (customerlist.length > 0) { //delete lastly client customerlist.length--; alert('the lastly client has been deleted.'); } else { alert('there no client delete!'); } } //identify delete client function identifythendeletecustomer() { //get client name var customername = prompt('enter name of client delete:'); //get client status var customerstatus = prompt('enter \'child\' or \'adult\':'); //check client status while (!(customerstatus == 'child' || customerstatus == 'adult')) { customerstatus = prompt('error - come in \'child\' or \'adult\':'); } //delete client deletecustomer(customername, customerstatus); } //delete client function deletecustomer(aname, astatus) { //create new array var newcustomerlist = new array(); //loop customers (var = 0; < customerlist.length; i++) { var client = customerlist[i]; //check client if ((customer.name != aname) || (customer.status != astatus)) { //add new user var newindex = newcustomerlist.length; newcustomerlist[newindex] = customer; } } //check deleted if (newcustomerlist.length < customerlist.length) { alert('the client has been deleted.'); } else { alert('there no client delete!'); } //update client list customerlist = newcustomerlist; }

so above can find solution, brandon mentioned, new array has been created, in each client added if client was not 1 looking for. hence leaving array without client looking for, new array replaces original one.

javascript arrays

Comments

Popular posts from this blog

xslt - DocBook 5 to PDF transform failing with error: "fo:flow" is missing child elements. Required content model: marker* -

mediawiki - How do I insert tables inside infoboxes on Wikia pages? -

Local Service User Logged into Windows -