Question

<!DOCTYPE html>
<html>
<body>
<script language="javascript" type="text/javascript">
//Definition of staff members (class)
function StaffMember(name,discountPercent){
    this.name = name;
    this.discountPercent = discountPercent;
}
//Creation of staff members (object)
var s121 = new StaffMember("Sally",5);
var b122 = new StaffMember("Bob",10);
var d123 = new StaffMember("Dave",20);
staffMembers = [s121,b122,d123];
//Creation of cash register (object)
var cashRegister = {
    total:0,
    lastTransactionAmount: 0,
    //Add to the total (method)
    add: function(itemCost){
        this.total += (itemCost || 0);
        this.lastTransactionAmount = itemCost;
    },
    //Retreive the value of an item (method)
    scan: function(item,quantity){
        switch (item){
        case "eggs": this.add(0.98 * quantity); break;
        case "milk": this.add(1.23 * quantity); break;
        case "magazine": this.add(4.99 * quantity); break;
        case "chocolate": this.add(0.45 * quantity); break;
        }
        return true;
    },
    //Void the last item (method)
    voidLastTransaction : function(){
        this.total -= this.lastTransactionAmount;
        this.lastTransactionAmount = 0;
    },
    //Apply a staff discount to the total (method)
    applyStaffDiscount: function(employee) {
        this.total -= this.total * (employee.discountPercent / 100);
    }

};
//Ask for number of items
do {
  var numOfItems = prompt("How many items do you have?");
  document.body.innerHTML = numOfItems;
  if (isNaN(numOfItems)) {
      i=0;
    } else {
      i=1;
    }
} while (i===0);
//Ask for item and qty of item
var items = [];
var qtys = [];
for(var i=0;i<numOfItems;i++) {
  var j=0;
  do {
  items[i] = prompt("What are you buying? (eggs, milk, magazine, chocolate)");
  switch (items[i]) {
    case "eggs" :;
    case "milk" :;
    case "magazine" :;
    case "chocolate" : j=1; document.body.innerHTML = items[i]; break;
    default : document.body.innerHTML = 'Item not reconized, please re-enter...'
; break;}
  } while (j===0);
  do {
    qtys[i] = prompt("How many " + items[i] + " are you buying?");
    document.body.innerHTML = qtys[i];
    if (isNaN(qtys[i])) {
      j=1;
    } else {
      j=0;
    }
  } while (j===1);
  //Add to the sub-total
  cashRegister.scan(items[i],qtys[i])
}
//Find out if it's a staff member & if so apply a discount
var customer;
var staffNo;
do {
  customer = prompt("Please enter customer name or type 'staff'.");
  document.body.innerHTML = customer;
  if (customer === 'staff') {
    staffNo = prompt("Please enter your staff number");
    for (i in staffMembers) {
      if (staffMembers[i] === staffNo) {
        cashRegister.applyStaffDiscount(staffNo);
      } else {
        document.body.innerHTML = "Staff number not found";
      };
    }
  }
  i=1;
} while (i=0);
// Show the total bill
if (customer !== 'staff') {
  document.body.innerHTML = 'Your bill is £'+cashRegister.total.toFixed(2)
  +' Thank you for visiting ' +customer;
} else {
  document.body.innerHTML = 'Your bill is £'+cashRegister.total.toFixed(2)
  +' Thank you for visiting ' +staffNo;
};

</script>
</body>
</html> 

What seems to be wrong with my code, it works but does not apply the staff discount, I feel the error is around;

    for (i in staffMembers) {
      if (staffMembers[i] === staffNo) {
        cashRegister.applyStaffDiscount(staffNo);
      } else {
        document.body.innerHTML = "Staff number not found";
      };
    }

Can anybody help spot the error, I've been learning on CodeAcademy but have taken the final example further with checks on data entered. But I can't seem to see why this section is not working as it should when checked via 'http://www.compileonline.com/try_javascript_online.php'.

Was it helpful?

Solution

It turns out there is quite a lot wrong with your code. The key things you need to fix to get it "working" are the following:

First - you are asking for a "staff number", but your employee structure doesn't have space for that. You could modify the StaffMember as follows:

//Definition of staff members (class)
function StaffMember(name, number, discountPercent){
    this.name = name;
    this.number = number;
    this.discountPercent = discountPercent;
}
//Creation of staff members (object)
var s121 = new StaffMember("Sally","s121",5);
var b122 = new StaffMember("Bob","b122",10);
var d123 = new StaffMember("Dave","d123",20);
staffMembers = [s121,b122,d123];

Now you have a "unique identifier" - I decided to give the employee the same number as the variable name, but that is not necessary.

Next, let's look at the loop you had: change it to

do {
  customer = prompt("Please enter customer name or type 'staff'.");
  document.body.innerHTML = customer;
  if (customer === 'staff') {
    staffNo = prompt("Please enter your staff number:");
    for (i in staffMembers) {
      if (staffMembers[i].number === staffNo) {  // <<<<< change the comparison
        cashRegister.applyStaffDiscount(staffMembers[i]);  // <<<<< call applyStaffDiscount with the right parameter: the object, not the staff number
      } else {
        document.body.innerHTML = "Staff number not found";
      };
    }
  }
  i=1;  // <<<<< I really don't understand why you have this do loop at all.
} while (i == 0); // <<<<< presumably you meant "while(i == 0)"? You had "while (i=0)"

There are many, many things you could do to make this better - but at least this will get you started. Complete "working" code (I may have made other edits that I forgot to point out - but the below is copied straight from my working space, and "works" - albeit rather flaky):

<!DOCTYPE html>
<html>
<body>
<script language="javascript" type="text/javascript">
//Definition of staff members (class)
function StaffMember(name, number, discountPercent){
    this.name = name;
    this.number = number;
    this.discountPercent = discountPercent;
}
//Creation of staff members (object)
var s121 = new StaffMember("Sally","s121",5);
var b122 = new StaffMember("Bob","b122",10);
var d123 = new StaffMember("Dave","d123",20);
staffMembers = [s121,b122,d123];
//Creation of cash register (object)
var cashRegister = {
    total:0,
    lastTransactionAmount: 0,
    //Add to the total (method)
    add: function(itemCost){
        this.total += (itemCost || 0);
        this.lastTransactionAmount = itemCost;
    },
    //Retreive the value of an item (method)
    scan: function(item,quantity){
        switch (item){
        case "eggs": this.add(0.98 * quantity); break;
        case "milk": this.add(1.23 * quantity); break;
        case "magazine": this.add(4.99 * quantity); break;
        case "chocolate": this.add(0.45 * quantity); break;
        }
        return true;
    },
    //Void the last item (method)
    voidLastTransaction : function(){
        this.total -= this.lastTransactionAmount;
        this.lastTransactionAmount = 0;
    },
    //Apply a staff discount to the total (method)
    applyStaffDiscount: function(employee) {
        this.total -= this.total * (employee.discountPercent / 100);
    }

};
//Ask for number of items
do {
  var numOfItems = prompt("How many items do you have?");
  document.body.innerHTML = numOfItems;
  if (isNaN(numOfItems)) {
      i=0;
    } else {
      i=1;
    }
} while (i===0);
//Ask for item and qty of item
var items = [];
var qtys = [];
for(var i=0;i<numOfItems;i++) {
  var j=0;
  do {
  items[i] = prompt("What are you buying? (eggs, milk, magazine, chocolate)");
  switch (items[i]) {
    case "eggs" :;
    case "milk" :;
    case "magazine" :;
    case "chocolate" : j=1; document.body.innerHTML = items[i]; break;
    default : document.body.innerHTML = 'Item not reconized, please re-enter...'
; break;}
  } while (j===0);
  do {
    qtys[i] = prompt("How many " + items[i] + " are you buying?");
    document.body.innerHTML = qtys[i];
    if (isNaN(qtys[i])) {
      j=1;
    } else {
      j=0;
    }
  } while (j===1);
  //Add to the sub-total
  cashRegister.scan(items[i],qtys[i])
}
//Find out if it's a staff member & if so apply a discount
var customer;
var staffNo;
do {
  customer = prompt("Please enter customer name or type 'staff'.");
  document.body.innerHTML = customer;
  if (customer === 'staff') {
    staffNo = prompt("Please enter your number:");
    for (i in staffMembers) {
      if (staffMembers[i].number === staffNo) {
        cashRegister.applyStaffDiscount(staffMembers[i]);
      } else {
        document.body.innerHTML = "Staff number not found";
      };
    }
  }
  i=1;
} while (i=0);
// Show the total bill
if (customer !== 'staff') {
  document.body.innerHTML = 'Your bill is £'+cashRegister.total.toFixed(2)
  +'<br> Thank you for visiting ' + customer;
} else {
  document.body.innerHTML = 'Your bill is £'+cashRegister.total.toFixed(2)
  +'<br> Thank you for visiting staff member ' +staffNo;
};

</script>
</body>
</html>

OTHER TIPS

In every singe loop there is an element from stattMembers in i, not its position. So you don't get 0, 1, 2 in it but staffMembers[0], staffMembers[1], staffMembers[2]. You should think about this, maybe a simple for-loop is better in your case?

The problem lies in these lines

if (staffMembers[i] === staffNo) {
        cashRegister.applyStaffDiscount(staffNo);
} 

it should have been

if (i === staffNo) {
        cashRegister.applyStaffDiscount(staffMembers[i]);
}
  1. Since you are first validating for the existence of the index(no) of a staff, then i == staffNo should be the right condition.
  2. cashRegister.applyStaffDiscount() expects an instance of a StaffMember so passing on to the staffMembers[i] object does the trick.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top