2

I am working on a program that records customer name and status(child/adult), the program allows add, display and delete customer records from array. However, if user enters the same name and status e.g:

Name: james, status: adult Name: james, status: adult

I want the function to delete just one record,but now it delete both of them, do i have to add break here? Please help.

PS: I can't use any inbuilt JavaScript functions such as slice(),delete(), concat(), join(), pop(), push(), reverse(), shift(), slice(), sort(), splice(), toString(), unshift() or valueOf()

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>

5
  • 1
    Maintaining a database means not keeping duplicate records. Would it not be better to prevent a duplicate entry in the first place. Commented Nov 13, 2014 at 18:14
  • Yes, but the problem here is that the program will be used by non-expert, so they might enter duplicate record. Commented Nov 13, 2014 at 18:22
  • Couldn't your program warn the user that the entry they have made already exists in the database? Commented Nov 13, 2014 at 18:28
  • I can't do that, They can enter duplicate records, but i am not sure how to just delete on of them, now it deletes both Commented Nov 13, 2014 at 18:29
  • possible duplicate of Javascript program - deleting an element from an array Commented Aug 28, 2015 at 11:14

1 Answer 1

1

You can make your delete function like this,

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!');
}

It will delete just one.

Since you said you cant use built in functions. In that case you have to copy the elements before and after the one to remove. You can have a control variable marking that you already found the one to delete. So no more deletions will happen.

For example,

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
}
Sign up to request clarification or add additional context in comments.

4 Comments

i can't use any inbuilt JavaScript funtions such as "slice()","delete()", "concat()", "join()", "pop()", "push()", "reverse()", "shift()", "slice()", "sort()", "splice()", "toString()", "unshift()" or "valueOf()
:O why not.. isn't it javascript.. what built in functions to you have access to?
Yeah, its an assignment for my university, and we can not use inbuilt javascript functions, i was wondering can we put like a counter in it? so it add the record back after delete both of them? so stucked
Thanks CaldasGSM, that really helped, god bless you :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.