How do I properly use the syntax for a setter method?


#1

Below is the example that is given for the problem at this link: click here

let restaurant = {
  _name: 'Italian Bistro',
  _seatingCapacity: 120,
  _hasDineInSpecial: true,
  _entrees: ['Penne alla Bolognese', 'Chicken Cacciatore', 'Linguine pesto'],

  set seatingCapacity(newCapacity) {
      if (typeof newCapacity === 'number') {
        this._seatingCapacity = newCapacity;
      console.log(`${newCapacity} is valid input.`);
    } else {
        console.log(`Change ${newCapacity} to a number.`)
    }
  }
}

Now if I run my code, that looks exactly the same except I use newAge, why am I getting an error message?

See below:

let person = {
  _name: 'Lu Xun',
  _age: 137,
  set Age(newAge){
    if (typeof newAge === 'number') 
    {this._age = newAge;} 
    console.log(`${newAge} is valid input.`);
  }
  else {console.log(`Change ${newAge} to a number.`) }
};

error: unexpected else


#2

_age: 137 is defined with lowercase a, so why is the setter method spelled as Age? Why the uppercase letter?

we talked about curly bracket style:

if (condition){

}
// or:
if (condition)
{

}

combined with proper indent, the error your currently have should be something you can handle/manage. You really need to start to with implementing some good habits (like curly bracket and indent)

your else is nested outside the age setter method.

It feels like things i am saying are not coming across. We can’t just endlessly help you with the same issues you keep running into. You need to learn to debug as well.


#3
let person = {///opening of objects
  _name: 'Lu Xun',
  _age: 137,
  set age(newage){///opening of setter
    if (typeof newage === 'number') 
    {this._age = newage; ///beginning of then statement
    
    } ///end of then statement
    console.log(`${newAge} is valid input.`);
  }///closing of setter
  
  else {console.log(`Change ${newAge} to a number.`) 
       } ///beginning of then statement, then end of then statement
}; ///closing of objects

I’m doing my indent properly and I am including the curly brackets for the return statement. Is this closer?


#4

The reality is that I’m learning how to do it the right way so, yes, I will make mistakes on a new language or using a language with more depth than before.


#5

I made sure my terminology was clear and have updated it during this time period while you were typing


#6

for if statement you do:

if (typeof newage === 'number') 
{
    this._age = newage; ///beginning of return statement
} ///end of return statement

but for else you do:

else {
    console.log(`Change ${newAge} to a number.`) 
}

you need to decide what style you prefer, and then use this style consistent throughout your program

There is a reason these two styles are most common. They provide best readability

And i am surprised, given the amount of forum posts you made, you haven’t found out how to use format to make the indent show on the forum.

why is your else statement outside your setter method? else always has to be after an if or else if statement? This basics JS, you should know this by know. I already mentioned it in my previous reply, but you didn’t respond to it

I know i am harsh, but this is basic. You should know how to use if, else if and else. Or how to debug it, or what the error message in the console means.


#7

Im grateful, but like I said my syntax with basic javascript is ok. I’m still learning to keep it with these additional methods and as I practice with projects, it will come. Rome was not built in a day and my understanding of this will not just happen out of thin air. The majority of the exercises I have answered on my own, a large majority as well. I know where you’re coming from, but this is just the way things are for right now.

I will only really learn these things once I make projects. Alot of people just give up. I’m not one of those people.


#8

but implementing a curly brackets style, is something which you can learn in 5 minutes. I already advised you about this earlier this week, if it wasn’t last week. This really makes things easier for yourself

yes, rome wasn’t build in a day, and you certainly shouldn’t give up.

else is basic, it should be after an if clause. Why are you struggling to place else at the right place then?


#10
let person = {///opening of objects
  _name: 'Lu Xun',
  _age: 137,
  set age(newage){///opening of setter
    if (typeof newage === 'number') 
    {this._age = newage; ///beginning of then statement
    
    } ///end of then statement
    console.log(`${newAge} is valid input.`) ///should I put a semicolon at the end of the if then statement?

  
  else {console.log(`Change ${newAge} to a number.`) 
       } ///beginning of then statement, and end of then statement
}///closing of setter, is this where it goes?
}; ///closing of objects

#11

I’ll compare the two answers (the final answer and my answer) and learn that way. Thanks for pushing me about syntax.


#12

your if and else are still using different curly brackets styles. Don’t put code on the same line after the {, like i showed you here:

if (typeof newage === 'number') 
{
    this._age = newage; ///beginning of return statement
} ///end of return statement

else {
    console.log(`Change ${newAge} to a number.`) 
}

and this what i mean, i suggest something, you are not implementing it. start using this curly brackets style, it will make your code more readable and you will profit from it. But use one of the two, not both.

that is really the most dreadful way to learn for things like programming and math. Because then you didn’t implement the solution yourself.


#13

What I noticed is that part of the error was that it wanted me to specifically
return 'invalid input.'
I can take a look at it again.


#14

Is it possible for me to use concatenation here somehow and to use this in the example, or am I reading too much into the problem?

That’s what I originally wanted to do


#15

that is well spotted :slight_smile:

But now i don’t feel like you heard what i said.


#16

I reset the problem and I want to try solve it my way. I heard you :slight_smile:


#17

why would you use concatenation? you use es6 string interpolation:

console.log(`Change ${newAge} to a number.`) 

the backticks (grave accent) and ${} are the indication that use es6 string interpolation. string interpolation is better then concatenation


#18

can you use that with a return statement? String interpolation I mean?


#19

of course, string interpolation is only a way to insert a variable in a string


#20
let person = {///opening of objects
  _name: 'Lu Xun',
  _age: 137,
  set age(newAge){///opening of setter
    if (typeof newage === 'number') 
    {
      this._age = newage; ///beginning of then statement
    console.log(`${newage} is valid input.`);
    } ///end of then statement
    
    ///should I put a semicolon at the end of the if then statement?

  
  else {
    console.log(`Change ${newage} to a number.`);
                   return "Invalid input"}
       } ///beginning of then statement, and end of then statement
}///closing of setter, is this where it goes?
}; 

If I want to see if the computer is running just the if part of my if statement, can I put a console log in the then part if the if statement?

and then again in the then part of the else statement?

Or will the computer read only the entire if statement?


#21

you are not paying attention, here:

else {
    console.log(`Change ${newage} to a number.`);
                   return "Invalid input"}
       } ///beginning of then statement, and end of then statement

the } ///beginning of then statement, and end of then statement doesn’t close the else statement (which you named then statement, interesting), given you have a } at the return line (one line above)

you really need be more careful

if you are lucky that the exercise validation calls the setter method, otherwise it never executes (or you have to use the setter method manual, just to be sure), then adding console.log statements make sense. Not that it would help you find this error