Files
.github
api-server
client
config
curriculum
challenges
_meta
arabic
chinese
english
01-responsive-web-design
02-javascript-algorithms-and-data-structures
basic-algorithm-scripting
basic-data-structures
access-an-arrays-contents-using-bracket-notation.english.md
access-property-names-with-bracket-notation.english.md
add-items-to-an-array-with-push-and-unshift.english.md
add-items-using-splice.english.md
add-key-value-pairs-to-javascript-objects.english.md
check-for-the-presence-of-an-element-with-indexof.english.md
check-if-an-object-has-a-property.english.md
combine-arrays-with-the-spread-operator.english.md
copy-an-array-with-the-spread-operator.english.md
copy-array-items-using-slice.english.md
create-complex-multi-dimensional-arrays.english.md
generate-an-array-of-all-object-keys-with-object.keys.english.md
iterate-through-all-an-arrays-items-using-for-loops.english.md
iterate-through-the-keys-of-an-object-with-a-for...in-statement.english.md
modify-an-array-stored-in-an-object.english.md
modify-an-object-nested-within-an-object.english.md
remove-items-from-an-array-with-pop-and-shift.english.md
remove-items-using-splice.english.md
use-an-array-to-store-a-collection-of-data.english.md
use-the-delete-keyword-to-remove-object-properties.english.md
basic-javascript
debugging
es6
functional-programming
intermediate-algorithm-scripting
javascript-algorithms-and-data-structures-projects
object-oriented-programming
regular-expressions
03-front-end-libraries
04-data-visualization
05-apis-and-microservices
06-information-security-and-quality-assurance
08-coding-interview-prep
09-certificates
portuguese
russian
spanish
formattingConversion
math-challenges
requiresTests
schema
test
.babelrc
.editorconfig
.npmignore
.travis.yml
CHANGELOG.md
LICENSE.md
commitizen.config.js
commitlint.config.js
create-challenge-bundle.js
getChallenges.js
gulpfile.js
index.js
lib.js
md-translation.js
package-entry.js
package-lock.json
package.json
utils.js
docs
guide
search-indexing
tools
.editorconfig
.eslintignore
.eslintrc.json
.gitattributes
.gitignore
.node-inspectorrc
.prettierignore
.prettierrc
.snyk
.travis.yml
.vcmrc
CODE_OF_CONDUCT.md
CONTRIBUTING.md
Dockerfile.tests
LICENSE.md
README.french.md
README.italian.md
README.korean.md
README.md
SECURITY.md
azure-pipelines.yml
change_volumes_owner.sh
docker-compose-shared.yml
docker-compose.tests.yml
docker-compose.yml
lerna.json
libcimp_index_js.patch
package-lock.json
package.json
patch_npm_and_install.sh
sample.env
freeCodeCamp/curriculum/challenges/english/02-javascript-algorithms-and-data-structures/basic-data-structures/iterate-through-the-keys-of-an-object-with-a-for...in-statement.english.md

147 lines
3.5 KiB
Markdown
Raw Normal View History

---
id: 587d7b7d367417b2b2512b1d
title: 'Iterate Through the Keys of an Object with a for...in Statement'
challengeType: 1
---
## Description
<section id='description'>
2019-04-26 17:21:49 -07:00
Sometimes you may need to iterate through all the keys within an object. This requires a specific syntax in JavaScript called a <dfn>for...in</dfn> statement. For our <code>users</code> object, this could look like:
```js
for (let user in users) {
console.log(user);
}
// logs:
Alan
Jeff
Sarah
Ryan
```
In this statement, we defined a variable <code>user</code>, and as you can see, this variable was reset during each iteration to each of the object's keys as the statement looped through the object, resulting in each user's name being printed to the console.
<strong>NOTE:</strong> Objects do not maintain an ordering to stored keys like arrays do; thus a key's position on an object, or the relative order in which it appears, is irrelevant when referencing or accessing that key.
</section>
## Instructions
<section id='instructions'>
We've defined a function <code>countOnline</code> which accepts one argument (a users object). Use a <dfn>for...in</dfn> statement within this function to loop through the users object passed into the function and return the number of users whose <code>online</code> property is set to <code>true</code>. An example of a users object which could be passed to <code>countOnline</code> is shown below. Each user will have an <code>online</code> property with either a <code>true</code> or <code>false</code> value.
```js
{
Alan: {
online: false
},
Jeff: {
online: true
},
Sarah: {
online: false
}
}
```
</section>
## Tests
<section id='tests'>
```yml
tests:
- text: The function <code>countOnline</code> should use a `for in` statement to iterate through the object keys of the object passed to it.
testString: assert(code.match(/for\s*\(\s*(var|let)\s+[a-zA-Z_$]\w*\s+in\s+[a-zA-Z_$]\w*\s*\)\s*{/));
- text: 'The function <code>countOnline</code> should return <code>1</code> when the object <code>{ Alan: { online: false }, Jeff: { online: true }, Sarah: { online: false } }</code> is passed to it'
testString: assert(countOnline(usersObj1) === 1);
- text: 'The function <code>countOnline</code> should return <code>2</code> when the object <code>{ Alan: { online: true }, Jeff: { online: false }, Sarah: { online: true } }</code> is passed to it'
testString: assert(countOnline(usersObj2) === 2);
- text: 'The function <code>countOnline</code> should return <code>0</code> when the object <code>{ Alan: { online: false }, Jeff: { online: false }, Sarah: { online: false } }</code> is passed to it'
testString: assert(countOnline(usersObj3) === 0);
```
</section>
## Challenge Seed
<section id='challengeSeed'>
<div id='js-seed'>
```js
function countOnline(usersObj) {
// change code below this line
// change code above this line
}
```
</div>
### After Test
<div id='js-teardown'>
```js
const usersObj1 = {
Alan: {
online: false
},
Jeff: {
online: true
},
Sarah: {
online: false
}
}
const usersObj2 = {
Alan: {
online: true
},
Jeff: {
online: false
},
Sarah: {
online: true
}
}
const usersObj3 = {
Alan: {
online: false
},
Jeff: {
online: false
},
Sarah: {
online: false
}
}
```
</div>
</section>
## Solution
<section id='solution'>
```js
function countOnline(usersObj) {
let online = 0;
for(let user in usersObj){
if(usersObj[user].online) {
online++;
}
}
return online;
}
```
</section>