JAVASCRIPT ITERATORS, OBJECTS, AND CLASSES: Build a Library

So I have a task to create a object method named toggleCheckOutStatus that changes the value saved to the _isCheckedOut property.

If the value is of _isCheckedOut is true , then change it to false . If the value is false , then change it to true.

The correct soution for thiss was:

toogleCheckOutStatus() {
  this._isCheckedOut = !this._isCheckedOut;
}

I understand the code but I want to write the method in different way using if…else:

 toggleCheckOutStatus(){
    if(this.isCheckedOut = true){
      this.isCheckedOut = false;
    } else {
      this.isCheckedOut = true;
    }
  }```

The above code is simply not working, when I call the method on object the properties doesn't change. Did I go wrong in the code or what?

Thanks in advance!

Hello, and welcome to the forums!

Look really closely at the property names in each solution. Are you sure you’re using the same one as the correct solution?

2 Likes

Hi,

Thanks for your response! Yeah I have tried the _isCheckedOut propertiese at the first place, but got the same result!

As I use toggleCheckOutStatus() methods, return result for isCheckedOut properties is still it’s initial value ‘false’.

Is it ok to use if…else function in method object?

It definitely is alright to use that in an object method. In addition to the property name issue mentioned, you also have a problem with your if condition. Look closely at that and make sure you are checking for equality and not accidentally doing something else.

1 Like