0
true and false into string
Hello I have class with 3 variables and one of them is boolean. class Book { constructor(title,autor) { this.title = title; this.autor = autor; this.readed = true; this.describeBook = function() { console.log( "Book has title " + this.title + " ,autor is " + this.autor + " and it's been " + this.readed) ; } } } var wiedzmin = new Book('Wiedźmin', 'Andrzej Sapkowski'); wiedzmin.describeBook(); And the output I need to get is " Book has title Wiedzmin, autor is Andrzej Sapkowski and it's been readed" But instead of "readed" I got "true" and don't have any clue how to sole this... Any help ? Thank you in advance
4 Antworten
+ 2
1. Make it a string instead of boolean. Then you can just store it as "read" or "not read," and use that value for your display easily.
2. You could run a check before the message is displayed, have it check if it's true/false, and then in a string variable have it store the correct message based upon if it was true or false.
+ 2
That's fine. Go with the other option then.
this.readed == true ? stringVar = "read" : stringVar = "not read"
Then just use your string variable in the display method.
0
ok but my task is to create class with 3 variables and one has to be boolean so i can't change it to string
0
like that ??
class Book {
constructor(title,autor) {
this.title = title;
this.autor = autor;
this.readed == true ? stringVar = "read" : stringVar = "not read";
this.describeBook = function() {
console.log( "Book has title " + this.title + " ,autor is " + this.autor + " and it's been " + stringVar) ;
}
}
}
var wiedzmin = new Book('Wiedźmin', 'Andrzej Sapkowski');
wiedzmin.describeBook();
i got that stringVar is not defined