6/7. Over The Moon


#1



Hey.Code works.Console say this "ReferenceError: discountPercent is not defined


// create a constructor for the StaffMember class
function StaffMember(name,discountPecent) {
    this.name = name;
    this.discountPercent = discountPercent;
};

var sally = new StaffMember("Sally",5);
var bob = new StaffMember("Bob",10);

//Create a StaffMember for yourself called me
var me = new StaffMember("Arthur",20);


Is it supposed to print that?


#2

@warriorcodes,

Have a close look at

discountPecent


#3

I cant figure out the problem


#4

Hi this part

function StaffMember(name,discountPecent)

You wrote discountPecent instead of discountPercent


#5

whats wrong with this code, im getting error like Oops try again false & in output im getting your bill is NaN

function StaffMember(name,discountPercent){
this.name = name;
this.discountPercent = discountPercent;
}

var sally = new StaffMember("Sally",5);
var bob = new StaffMember("Bob",10);

// Create yourself again as 'me' with a staff discount of 20%
var me = new StaffMember("Pavitha",20);

var cashRegister = {
total:0,
lastTransactionAmount: 0,
add: function(itemCost){
this.total += (itemCost || 0);
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;
},
voidLastTransaction : function(){
this.total -= this.lastTransactionAmount;
this.lastTransactionAmount = 0;
},
// Create a new method applyStaffDiscount here
applyStaffDiscount: function(employee) {
this.total -= this.total * (employee.discountpercent / 100);
}
};

cashRegister.scan('eggs',1);
cashRegister.scan('milk',1);
cashRegister.scan('magazine',3);
// Apply your staff discount by passing the 'me' object
// to applyStaffDiscount
cashRegister.applyStaffDiscount(me);

// Show the total bill
console.log('Your bill is '+cashRegister.total.toFixed(2));


#6

Hi this part

applyStaffDiscount: function(employee) {
    this.total -= this.total * (employee.discountpercent / 100);
}

you wrote discountpercent instead of discountPercent with the uppercase on Percent


#7

I corrected that error but still its showing same error.


#8

can you post your new code?


#9

function StaffMember(name,discountPercent){
this.name = name;
this.discountPercent = discountPercent;
}

var sally = new StaffMember("Sally",5);
var bob = new StaffMember("Bob",10);

// Create yourself again as 'me' with a staff discount of 20%
var me = new StaffMember("Pavitha",20);

var cashRegister = {
total:0,
lastTransactionAmount: 0,
add: function(itemCost){
this.total += (itemCost || 0);
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;
},
voidLastTransaction : function(){
this.total -= this.lastTransactionAmount;
this.lastTransactionAmount = 0;
},
// Create a new method applyStaffDiscount here
applyStaffDiscount: function(employee) {
this.total -= this.total * (employee.discountPercent / 100);
}
};

cashRegister.scan('eggs',1);
cashRegister.scan('milk',1);
cashRegister.scan('magazine',3);
// Apply your staff discount by passing the 'me' object
// to applyStaffDiscount
cashRegister.applyStaffDiscount('me');

// Show the total bill
console.log('Your bill is '+cashRegister.total.toFixed(2));


#10

you should remove the " " around me here

cashRegister.applyStaffDiscount('me');

#11

Thank you so much i got it.


#12

So, my code works and everything, and maybe this is just because I've been away a while and came back trying to do this cold, but I don't understand how "employee" is a valid way to do this since we never defined employee. There was no StaffMember.___ or a case situation or anything. Please, how did we get around this?


#13

@litppunk,
What you must understand
employee is a so-called parameter
this employee parameter will be used as a =local= variable
in the FUNCTION-BODY.

Now if you call the Method/Function
you provide a so-called argument which is the object Value me

Now during the execution of the Method/Function
employee is referring to the object me.

==============================================

the FUNCTION talk

var myFunc = function( param1, param2) {
       //Begin of  anonymous FUNCTION-BODY
       //VARIABLE -myFunc- has an -anonymous function- assigned
       //this -anonymous function- has 2 PARAMETERS param1 and param2
       //param1 and param2 PARAMETERS are used 
       //as -local- VARIABLES throughout the FUNCTION-BODY

      console.log( param1 + " and " + param2 ) ;

      //End of anonymous FUNCTION-BODY
};

If you want to call/execute the anonymous function
you will have to add a pair of parentheses to the variable myFunc
like
myFunc();
As the anonymous function was defined
as having 2 parameters
you have to provide 2 arguments
in our case 2 string VALUES "Alena" and "Lauren"
like
myFunc("Alena","Lauren");

some quotes from the outer-world:

**argument is the value/variable/reference being passed in,
parameter is the receiving variable used within the function/block**

OR

**"parameters" are called "formal parameters",
while "arguments" are called "actual parameters".**

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

function with 1 parameter using return-statement

var myFunction = function( param1 ) {
       //Begin of FUNCTION-BODY
       //myFunction =function= has 1 PARAMETER param1
       //this param1 PARAMETER is used as a -local- VARIABLE
       //throughout the FUNCTION-BODY

      return param1;

      //End of FUNCTION-BODY
      };

you have defined a myFunction function
which takes 1 parameter param1
this param1 parameter is used
as a variable throughout the FUNCTION-BODY.

If you want to call/execute this myFunction function
and this myFunction function was defined
as having 1 parameter param1
you will have to provide 1 argument
in our case a "number VALUE" 4
myFunction( 4 );

some quotes from the outer-world:

**argument is the value/variable/reference being passed in,
parameter is the receiving variable used within the function/block**

OR

**"parameters" are called "formal parameters",
while "arguments" are called "actual parameters".**

============================================

As you are using the return-statement in your myFunction function
you will only get a return-value no-display.
You can however capture this return-value in a variable
and then use the console.log()-method to do a display.

var theResult = myFunction( 4 );
console.log( theResult );

OR directly

console.log( myFunction( 4 ) );

#14

OH ok. I think i get it now.
Seems like there should be an easier way to explain that employee is just an unassigned stand in variable for parameter 1: name.

still we call it using:

employee.discount

which would imply that discount is sub to employee, not a parallel parameter

  1. List item 1
    sub item A
    sub item B

  2. List item 2
    sub item A
    sub item B

So somehow we called B using A, all fine and good, but we did it by calling it like it was categorized under A (1.A)rather than beside it. (A, B)

I guess the call method for both is the same then? Seems like calling a parallel item should be written different than a parent.child one. I guess that is what it was that had me so tripped up. Thanks.


#15

@litppunk,
The
employee parameter
is a =local= variable inside the Method/Function
and will get an object Value assigned
as soon as you call the applyStaffDiscount() Method
providing an argument me

You created the me object
using

function StaffMember(name,discountPercent){
 this.name = name;
 this.discountPercent = discountPercent;
}
var me = new StaffMember("Pavitha",20);
console.log(me)

and you then had the object

{ name: 'Pavitha', discountPercent: 20 }

and with the so-called dot-access-method
you can get access to the name and the discountPercent properties
like
me.name
or
me.discountPercent.

+++++++++++++++++++++++++++

A Javascript object
is a Ruby hash-table
is a Python dictionary


#16

Seriously? Lol...I would have thought Leon's writing was big enough...it was fairly clear.