Learn JavaScript: Objects - Getters and Setters 1


#1

My code isn’t passing the second test and I can’t figure out what I’m doing wrong…

"Inside of the setter we want to check if the user’s input is valid.

Create a conditional statement to check if a user’s input is a number.

If the input is a number, set the _age property to the input value. If the input is not a number, return the string, ‘Invalid input’."

let person = {
  _name: 'Lu Xun',
  _age: 137,
  
  set age(newAge) {
   if (typeof newAge === `number`) {
     this._age = newAge
   } else {
     return "Invalid input";
   }
  }
};

#2

What error message are you seeing? Also, please link to the exercise.


#3

So weird… now it’s passing and I have no idea what I changed.


#4

There are no errors in your code, so it should pass. Did you change the backticks to quotes? If not, then it would be difficult to guess what the SCT was rejecting. The backticks are valid string delimiters so it’s not a mistake, per se, but the SCT may not have been written to accept them. Mind, this is just a guess.

One other minor syntax error is the missing semi-colon after the line,

 this._age = newAge

#5

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.