I'm working on a JavaScript short program (trampoline hiring), the purpose of the program is to record according to the name of each customer currently on the trampoline and whether they are an adult or a child (status type).
Currently I have addcustomer, displayallcustomer and deletelastcustomer functions, however I am not sure what I have done wrong for the displayCustomerType(statusType) function. This function is for when I press Display Only Child Customers or Display Only Adult Customers, it will only display child or adult from the array list.
Demo on Fiddle
<html>
<head>
<meta charset="utf-8" />
<title>work 2</title>
<script type="text/javascript">
//maximum customer on the trampoline is 5
const MAX_CUSTOMERS = 5;
//create new Array
var customerList = new Array();
//add customer
function addCustomer() {
//check max customers
if (customerList.length >= MAX_CUSTOMERS) {
alert('Sorry, no more than ' + String(MAX_CUSTOMERS) + ' customers are allowed on the trampoline.');
} else {
//add new user
var newIndex = customerList.length;
customerList[newIndex] = new Object;
//ask user enter their name
customerList[newIndex].name = prompt('What is the customer\'s name?');
//ask user enter their status
customerList[newIndex].status = prompt('Are you a Child or an Adult?');
//check user is child or adult
while (!(customerList[newIndex].status == 'child' || customerList[newIndex].status == 'adult')) {
customerList[newIndex].status = (
prompt('Error Please Enter \'child\' or \'adult\':'));
}
}
}
//display customers
function displayAllCustomers() {
//create message
var message = '';
//loop customers
for (var i = 0; i < customerList.length; i++) {
//add customer to message
message += customerList[i].name + ', Status: ' + String(customerList[i].status) + '. \n';
}
//check message
if (message == '') {
message = 'There are no customer to display!';
}
//output message
alert(message);
}
//delete last customer
function deleteLastCustomer() {
//check customer list
if (customerList.length > 0) {
//delete last customer
customerList.length--;
alert('The last customer has been deleted.');
} else {
alert('There are no customer to delete!');
}
}
function displayCustomerType(statusType) {
var message = '';
for (var i = 0; i < customerList.length; i++) { //loop through customerlist
message += customerList[i].status + '. ';
}
if ((customerList[i].status = 'child') || (customerList[i].status = 'adult')) {
alert(message);
}
}
</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="deleteLastCustomer();">Delete Last Customer</button>
<br>
<button type="button" onclick="displayCustomerType('child');">Display Only Child Customers</button>
<br>
<button type="button" onclick="displayCustomerType('adult');">Display Only Adult Customers</button>
<br>
</div>
</body>
</html>