Web Analytics Made Easy -
StatCounter Fairly basic OO question - CodingForum

Announcement

Collapse
No announcement yet.

Fairly basic OO question

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Fairly basic OO question

    Hi all I wanted to ask something about some basic JS code in regard to OOP. In the code below I suspect that the add method states the following (and I would like to hear if I am correct) :

    - add is a function that takes itemCost as a parameter
    - add will increase the value of the property called total (that is tied to the object cashRegister) with Itemcost
    - add creates a new property bound to the object cashRegister called lastTransactionAmount which bares the value of itemCost

    thanks all!

    Code:
    var cashRegister = {
        total:0,
        //Dont forget to add your property
        add: function(itemCost ) {
            this.total +=  itemCost ;
            this.lastTransactionAmount = itemCost;
            
        },
        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;
        },
        //Add the voidLastTransaction Method here
        voidLastTransaction: function(lastTransactionAmount)  {
        this.total -= this.lastTransactionAmount;
        }
    };
    
    cashRegister.scan('eggs',1);
    cashRegister.scan('milk',1);
    cashRegister.scan('magazine',1);
    cashRegister.scan('chocolate',4);
    
    //Void the last transaction and then add 3 instead
    cashRegister.voidLastTransaction(3*0.45);
    cashRegister.scan('chocolate',3);
    //Show the total bill
    console.log('Your bill is '+cashRegister.total);
    I have been in love with my computer for about a year. Her patience with me is endless, for often times I have scolded her, accusing her of not working. But never, not once, has she lied to me or been unfaithful...

  • #2
    add will increase the value of the property called total (that is tied to the object cashRegister) with Itemcost
    or use string concatenation, depending on the data type of itemCost and total.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

    Comment


    • #3
      Allright thanks very much for your feedback, do you agree that the rest of my assumptions are correct? I would like some confirmation because I feel that if I dont get this elementary stuff right the foundation of my skills as a programmer will be weak
      I have been in love with my computer for about a year. Her patience with me is endless, for often times I have scolded her, accusing her of not working. But never, not once, has she lied to me or been unfaithful...

      Comment


      • #4
        yes, otherwise I see no fault in your reasoning.
        The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
        André Behrens, NY Times Software Developer

        Comment

        Working...
        X