diff --git a/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 b/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
index c6dd30cbe6..6808afd4cc 100644
--- a/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
+++ b/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
@@ -6,15 +6,47 @@ challengeType: 1
## Description
users
object, this could look like:
-for (let user in users) {
+Sometimes you may need to iterate through all the keys within an object. This requires a specific syntax in JavaScript called a for...in statement. In the example below, there is a
console.log(user);
}
// logs:
Alan
Jeff
Sarah
Ryanusers
object and the loop iterates through it to display the user's name to the console.
+
+const users = {
In this statement, we defined a variable
+ Alan: {
+ age: 30
+ },
+ Jeff: {
+ age: 45
+ },
+ Sarah: {
+ age: 18
+ }
+ Ryan: {
+ age: 24
+ }
+};
+for (let user in users) {
+ console.log(user);
+}
+// logs:
Alan
Jeff
Sarah
Ryan
+user
, 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.
-NOTE:
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.
+NOTE: 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.
countOnline
; use a for...in statement within this function to loop through the users in the users
object and return the number of users whose online
property is set to true
.
+We've defined a function countOnline
which accepts one argument (a users object). Use a for...in statement within this function to loop through the users object passed into the function and return the number of users whose online
property is set to true
. An example of a users object which could be passed to countOnline
is shown below. Each user will have an online
property with either a true
or false
value.
+
+
+{
+ Alan: {
+ online: false
+ },
+ Jeff: {
+ online: true
+ },
+ Sarah: {
+ online: false
+ }
+}countOnline
; use a for...in st
```yml
tests:
- - text: The users
object contains users Jeff
and Ryan
with online
set to true
and users Alan
and Sarah
with online
set to false
- testString: assert(users.Alan.online === false && users.Jeff.online === true && users.Sarah.online === false && users.Ryan.online === true, 'The users
object contains users Jeff
and Ryan
with online
set to true
and users Alan
and Sarah
with online
set to false
');
- - text: The function countOnline
returns the number of users with the online
property set to true
- testString: 'assert((function() { users.Harry = {online: true}; users.Sam = {online: true}; users.Carl = {online: true}; return countOnline(users) })() === 5, ''The function countOnline
returns the number of users with the online
property set to true
'');'
-
+ - text: The function countOnline
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 countOnline
should return 1
when the object { Alan: { online: false }, Jeff: { online: true }, Sarah: { online: false } }
is passed to it'
+ testString: assert(countOnline(usersObj1) === 1);
+ - text: 'The function countOnline
should return 2
when the object { Alan: { online: true }, Jeff: { online: false }, Sarah: { online: true } }
is passed to it'
+ testString: assert(countOnline(usersObj2) === 2);
+ - text: 'The function countOnline
should return 0
when the object { Alan: { online: false }, Jeff: { online: false }, Sarah: { online: false } }
is passed to it'
+ testString: assert(countOnline(usersObj3) === 0);
```
@@ -37,36 +72,58 @@ tests: