Skip to content

Commit 0b3bd51

Browse files
committed
iterate over object values correctly to log all properties
- Replaced invalid for...of loop on object with Object.values() - Ensures all property values of author are logged without errors
1 parent c87010e commit 0b3bd51

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

Sprint-2/debug/author.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
// Predict and explain first...
2+
// The code attempts to loop over author using for...of.
3+
// But for...of works on iterable objects like arrays, strings, Maps, Sets.
4+
// Plain objects ({}) are NOT iterable, so for (const value of author) will throw a TypeError:
5+
// author is not iterable.
26

37
// This program attempts to log out all the property values in the object.
48
// But it isn't working. Explain why first and then fix the problem
@@ -11,6 +15,6 @@ const author = {
1115
alive: true,
1216
};
1317

14-
for (const value of author) {
15-
console.log(value);
18+
for (const key in author) {
19+
console.log(author[key]);
1620
}

0 commit comments

Comments
 (0)