Skip to content

Instantly share code, notes, and snippets.

@virtualjess
Last active September 24, 2020 16:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save virtualjess/761d6ca9a343fe744f14a073cc875ffe to your computer and use it in GitHub Desktop.
Save virtualjess/761d6ca9a343fe744f14a073cc875ffe to your computer and use it in GitHub Desktop.
// attempt 1 of 2
// Codecademy throws the following text:
// "The generatePaycheck() function should logs(sic) to the console the hoursWorked multiplied by the hourlyWage .
// It should then set the hoursWorked to 0 ."
class Employee {
constructor(name, hourlyWage) {
this._name = name;
this._hourlyWage = hourlyWage;
this._hoursWorked = 0;
}
get name() {
return this._name;
}
get hourlyWage() {
return this._hourlyWage;
}
get hoursWorked() {
return this._hoursWorked;
}
set hoursWorked(hoursWorked) {
this._hoursWorked = hoursWorked
}
logHours (num) {
this._hoursWorked += num;
}
generatePaycheck() {
let payCheck = this._hoursWorked * this._hourlyWage;
return console.log(payCheck);
hoursWorked = 0;
}
};
//attempt 2; fixed the return on generatePaycheck and potential variable scoping problem with hoursWorked
// sadly it returns nothing at all, and they don't show you the solution
// or what tests you're failing, it just doesn't let you proceed with the quiz
// so I am still not sure whay it isn't working!
class Employee {
constructor(name, hourlyWage) {
this._name = name;
this._hourlyWage = hourlyWage;
this._hoursWorked = 0;
}
get name() {
return this._name;
}
get hourlyWage() {
return this._hourlyWage;
}
get hoursWorked() {
return this._hoursWorked;
}
set hoursWorked(workedHours) {
this._hoursWorked = workedHours;
}
logHours (num) {
this._hoursWorked += num;
}
generatePaycheck() {
let payCheck = this._hoursWorked * this._hourlyWage;
console.log(payCheck);
hoursWorked = 0;
}
};
@notmatt
Copy link

notmatt commented Sep 24, 2020

Differences between properties and methods (including setters/getters)

class Employee {
  constructor(name, wage) {
    // we're going to assume the name is "first name" and "last name"
    let names = name.split(" ");
    // first name is the first one
    this.prenom = names.shift();
    // "last name 
    this.surnom = names.join(" ");
  }

  get name() {
    return this.prenom + " " + this.surnom;
  }

  get firstName() {
    return this.prenom;
  }
}

function fakeConstructor(name, wage) {
  let names = name.split(" ");
  let newEmployee = {
    // set properties
    prenom : names.shift(),
    surnom : names.join(" "),
    get name() { /* ... */ },
    get firstName() { /* ... */ },
  };
  return newEmployee;
}

const e1 = new Employee("Matt Smillie", 10);

// these two are the same!
e1.firstName; // still works!
e1._firstName; // does not work!

// no equivalent property to this
e1.name

@virtualjess
Copy link
Author

// correct answer
generatePaycheck() {
let payCheck = this.hoursWorked * this.hourlyWage; //using getters to get the values
console.log(payCheck);
this.hoursWorked = 0; //using the setter to set the value
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment