1- Iterate Through the Keys of an Object with a for...in Statement

Tell us what’s happening:

Could Please check my code, really I am not sure, Been honest almost guessing some parts.
my code past the test. Why?
I read a lot of: Object.entries, values, keys.
examples in MDN, 3school and other pages.

Your code so far


let users = {
  Alan: {
    age: 27,
    online: false
  },
  Jeff: {
    age: 32,
    online: true
  },
  Sarah: {
    age: 48,
    online: false
  },
  Ryan: {
    age: 19,
    online: true
  }
};

function countOnline(obj) {
  // change code below this line
let userOnline = 4;
for(let user in obj){
if(obj[user].userOnline = true){  
userOnline++; 
return userOnline;

}
};
  // change code above this line
}

console.log(countOnline(users));

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-data-structures/-iterate-through-the-keys-of-an-object-with-a-for---in-statement

There are many errors here, I’ll just point them out but really I suggest you go back and review the lessons prior to this challenge well before you go any further

let userOnline = 4; // why are you setting this ?
   for(let user in obj){ // what is obj ??
      if(obj[user].userOnline = true){   //what is the difference between a comparison and assignment operator ??
          userOnline++; 
        return userOnline; // you are returning out of the for loop why ?
     }
};

I really emphasize that you go back to the previous lessons like these and come back with questions

https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-data-structures/add-key-value-pairs-to-javascript-objects
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-data-structures/access-property-names-with-bracket-notation
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-data-structures/check-if-an-object-has-a-property
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/comparison-with-the-equality-operator

2 Likes

Hey I did my Homework,

let foods = {
  apples: 25,
  oranges: 32,
  plums: 28
};


// change code below this line

foods["bananas"] =13;

foods["grapes"]=35;

foods["strawberries"]=27;

// change code above this line

console.log(foods);

let foods = {
  apples: 25,
  oranges: 32,
  plums: 28,
  bananas: 13,
  grapes: 35,
  strawberries: 27
};

// do not change code above this line

function checkInventory(scannedItem) {
  
// change code below this line

let inventory = foods[scannedItem];
return inventory;
}


// change code below this line to test different cases:

console.log(checkInventory("apples"));

let users = {
  Alan: {
    age: 27,
    online: true
  },
  Jeff: 
{
    age: 32,
    online: true
  },
  
Sarah: {
    age: 48,
    online: true
  },
  
Ryan: {
    age: 19,
    online: true
  }
};


function isEveryoneHere(obj) {
  
// change code below this line

if(users.hasOwnProperty("Alan","Jeff","Sarah","Ryan"))
{
return true;

} 

return false;
  
// change code above this line



console.log(isEveryoneHere(users));

// Setup 
function testEqual(val) {
  if (val == 12) 
{ // Change this line
   
 return "Equal";
 
 }
  return "Not Equal";

}

// Change this value to test
testEqual(10);

I try this way but not worked:
let users = {
  Alan: {
    age: 27,
    online: false
  },
  Jeff: {
    age: 32,
    online: true
  },
  Sarah: {
    age: 48,
    online: false
  },
  Ryan: {
    age: 19,
    online: true
  }
};

function countOnline(obj) {
  // change code below this line
let userOnline = 0;
for(let user in obj){
if(obj[user].hasOwnProperty('online')){
if(obj[user].userOnline[] = true){  
userOnline++;
return userOnline;  
}
}
}
  // change code above this line
}

console.log(countOnline(users));

Watch the lastnight solutions:
let users = {
  Alan: {
    age: 27,
    online: false
  },
  Jeff: {
    age: 32,
    online: true
  },
  Sarah: {
    age: 48,
    online: false
  },
  Ryan: {
    age: 19,
    online: true
  }
};

function countOnline(obj) {
  // change code below this line
let userOnline = 4;
for(let user in obj){
if(obj[user].hasOwnProperty('online')){
if(obj[user].userOnline = true){  
userOnline++;
return userOnline;  
}
}
}
  // change code above this line
}

console.log(countOnline(users));

console logs: 5 and worked,
I am doing something wrong?
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-data-structures/-iterate-through-the-keys-of-an-object-with-a-for---in-statement”.

You have like 7 different challenges in a post, none of them correctly formatted, it is a bit difficult to be able to help you with this
To format your code, select a block of code in the message editor and then use the “Preformatted text” </> button, so that it will be surrounded by backticks

Let’s see this last one:

You can’t know how many users are online, so you should initialise userOnline to 0.
You are also using if(obj[user].userOnline = true) as a condition, and as you are using an assignment operator and not a comparison this will never be false
Then you have a return statement inside s loop, that means that the first iteration in which the return is executed the function stops and a value is returned from the function, effectively counting only the first online user.
You have a few issues to solve here

Sorry, I just trying to do the right thing, and not to be a dump for not asking.
I find a video on youtube and helps me.

bye

If you need more explanation for anything that I said you can ask again, I was explaining what’s so that you can learn how to solve the issues

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make easier to read.

See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.

Note: Backticks are not single quotes.

markdown_Forums

2 Likes