So i know there is a proper way to post code on here that I have a question about. I remember I need to use the backticks before and after the code. Is there anything else I need to do ?
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.
also, your current code, the challenge link, or instructions of what you need to do (exactly as writte ), what you think the challenge is asking you to do, failing tests, what you understand and not understandā¦
also, describing what you think each piece of code is doing (commenting your code even if you donāt need to ask a question is good practice)
these are all things that let you ask a better question and so get a better answer
Okay thank you. And first let me also thank you for being quick to respond. And always being here.
So I dont always word questions correctly, but like everything else I am working on it.
But in the Basic Data Structures: Iterate Through the Keys of an Object with a forā¦in Statement challenge.
I am wondering how the word name is recognized or understood in the program, I guess with out it being defined.
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 count = 0;
for (let name in users){
if (users[name].online === true){
count ++;
}
}
return count;
// change code above this line
}```
console.log(countOnline(users));`
How dose it understand ` (let name in users` or `users[name]
Also it still says to wrap in backticks I thought I did. So I apologize cant seem to get it rite
Thank you for your response and help. It is much appreciated.
Youāre fine, no worries. Perfectly understandable question. Letās break it down.
First, the top-level object, users
, looks like this (just the root object and itās own direct properties):
users = {
Alan: /* user sub-object */,
Jeff: /* user sub-object */,
Sarah: /* user sub-object */,
Ryan: /* user sub-object */
}
So thatās the basic users
object. When we do the line
for (let name in users){ ...}
Weāre saying, āfor each of the properties of the users object, take that property name and assign that, as a string value, to the variable name
, then do this loop.ā So we are iterating over the list of users. There is no āindex orderā enforcement, so we canāt count on Alan
being first, but letās pretend he is.
The first pass through our for...in
loop, the string value Alan
is assigned to the variable name
. Within the scope of that for loop, name
is the string āAlanā.
At each iteration, we get another direct child property of that users
object, and assign the property name to our variable, and do the same loop.
Learn more about for...in
on devDocs.
Thank you very much. You are always a great help, and I truly appreciate it.
Nah. Iām just glad to have good questions to answer. Keep 'em coming!!
hereā¦ where?
Ooops. Forgot that link. Thanks for keeping me honest! rofl
All fixed, the link to the devdocs page is in there now.