true and false into string | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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

24th Aug 2017, 12:57 PM
Maciej Mazurek
Maciej Mazurek - avatar
4 Answers
+ 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.
24th Aug 2017, 1:07 PM
AgentSmith
+ 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.
24th Aug 2017, 1:22 PM
AgentSmith
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
24th Aug 2017, 1:12 PM
Maciej Mazurek
Maciej Mazurek - avatar
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
24th Aug 2017, 2:34 PM
Maciej Mazurek
Maciej Mazurek - avatar