Click here to Skip to main content
15,845,785 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello Everyone,

I am solving some assignment but I don't understand how to solve this task.
let classroom = {
    numOfStudents: 0,
    schoolHours: false,
    playtime: false,
    openSchool: function() {
        this.schoolHours = true;
        this.numOfStudents = 20;

*Inside the breakTime function, and using the this keyword where necessary, check if the key schoolHours is true, and if it is, set the key playtime to true

What I have tried:

let classroom = {
    numOfStudents: 0,
    schoolHours: false,
    playtime: false,
    openSchool: function() {
        this.schoolHours = true;
        this.numOfStudents = 20;
    },
    breakTime: function() {
       
           
     this.playTime= true;   
       
    }
};


classroom.openSchool(); 
console.log(classroom.numOfStudents);
classroom.breakTime();
console.log(classroom.playTime);
Posted
Updated 14-May-23 12:24pm

Quote:
check if the key schoolHours is true
Which means you need an if test.
 
Share this answer
 
Comments
adla099 24-Apr-23 11:40am    
Thanks for your answer. Please what goes inside the if () {
Richard MacCutchan 24-Apr-23 11:57am    
The question tells you.
adla099 24-Apr-23 12:04pm    
I have tried everything. 'Does the function breakTime work correctly? did you use the this key word correctly in the appropriate places?'
Richard MacCutchan 24-Apr-23 12:07pm    
I have tried everything.
Well, obviously not, since you have not added the if statement to the function. I suggest you take as look at JavaScript if else else if[^].
adla099 24-Apr-23 12:51pm    
let classroom = {
numOfStudents: 0,
schoolHours: false,
playtime: false,
openSchool: function() {
this.schoolHours = true;
this.numOfStudents = 20;

breakTime: function(){
if(){
playtime=true;

Nothing placed in the if works. Could you please provide me the solution. got undefined
To add to what Richard has said, this always refers to the current instance of the class it is used in: it allows you to be specific as with which of several variables of the same name you intended to use. For example if you have a method with a parameter called foo and a class level variable also called foo, then inside the method the parameter takes precedence and you need this.foo to access the class level variable.

See here: JavaScript this[^]
 
Share this answer
 
Comments
adla099 24-Apr-23 13:38pm    
Hi, thanks for your reply. Solution will do. Solved almost all the other questions. Dont know why this and one other question is confusing.
JavaScript
breakTime:function(){
        if(this.schoolHours){
            this.playtime = true;
        }
    }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900