The program I wrote is about how a sports equipment company monitor the trampoline use; it records the customer NAME, and their STATUS (child or adult) that are currently on the trampoline. There are five functions, so we can add customer, display customer and delete the last customer. I am stuck on the last function where I have to use object constructor to identify and then delete the customer.
PS: I can't use any predefined JavaScript array element-deleting or manipulating methods such as delete()
, concat()
, join()
, pop()
, push()
const MAX_CUSTOMERS = 5; //maximum customer on the trampoline is 5
var customerList = new Array();//create new Array
function addCustomer ()
{
if (customerList.length >= MAX_CUSTOMERS)
alert('Sorry, no more than ' + String(MAX_CUSTOMERS) + ' customers are allowed on the trampoline.')
else
{
var newIndex = customerList.length;
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'))
{
customerList[newIndex].status = (prompt('Error Please Enter \'child\' or \'adult\':'));
}
}
}
function displayAllCustomers ()
{
var message = '';
for (var i = 0 ; i < customerList.length ; i++)
{
message += customerList[i].name + ', Status: '
+ String(customerList[i].status) + '. \n';
}
if (message == '')
message = 'There are no customer to display!';
alert(message);
}
function deleteLastCustomer ()
{
if (customerList.length > 0)
{
customerList.length--;
alert('The last customer has been deleted.');
}
else
alert('There are no customer to delete!');
}
function identifyThenDeleteCustomer ()
{
var customerName = prompt('Enter the name of the customer to delete:');
var customerStatus = prompt('Enter \'child\' or \'adult\':');
while (!(customerStatus == 'child' || customerStatus == 'adult'))
customerStatus = prompt('Error - enter \'child\' or \'adult\':');
deleteCustomer(customerName,customerStatus);
}
function deleteCustomer (aName, aStatus)
{
;
}
Solution
So above you can find the solution, as Brandon mentioned, a new array has been created, in which each customer was added if this customer was not the one you were looking for. Therefore leaving you with an array without the customer you were looking for, this new array then replaces your original one.
You could return a new array.
JSFiddle: http://jsfiddle.net/Lf2e85ed/3/
Edit: Added comments to clarify. Is that better? The idea is that you can recreate the functionality of a splice() method by creating a new array of fruits, adding all of the fruit which are not the deleted fruit, and returning that new array.
In this case, we deleteFruit('apple'). Therefore, we cycle through the list of fruits (apple, banana, carrot). For each fruit, if it is not an apple, we add it to the new array of fruits. That means our new array of fruits contains banana and carrot. The function returns the new array of fruits, and it's assigned to the old array of fruits.
If you start with three fruits, and then you end up with two fruits, you've deleted one. You don't have to use splice(). In fact, it wouldn't surprise me if functions like splice() perform their functionality in a way similar to this, though the people who invented splice() surely did a better job than I did.
I hope this helps.
P.S. A carrot is a fruit now. :)