-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #266 from benjibuiltit/object-keys
added object keys challenge
- Loading branch information
Showing
6 changed files
with
59 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,44 @@ | ||
--- | ||
JavaScript provides a native way of listing all the available keys of an object. This can be helpful for looping through all the properties of an object and manipulating their values accordingly. | ||
|
||
# | ||
Here's an example of listing all object keys using the **Object.keys()** | ||
prototype method. | ||
|
||
--- | ||
```js | ||
const car = { | ||
make: 'Toyota', | ||
model: 'Camry', | ||
year: 2020 | ||
}; | ||
const keys = Object.keys(car); | ||
|
||
console.log(keys); | ||
``` | ||
|
||
The above code will print an array of strings, where each string is a key in the car object. `['make', 'model', 'year']` | ||
|
||
## The challenge: | ||
|
||
Create a file named `object-keys.js`. | ||
|
||
In that file, define a variable named `car` like this: | ||
|
||
```js | ||
const car = { | ||
make: 'Honda', | ||
model: 'Accord', | ||
year: 2020 | ||
}; | ||
``` | ||
|
||
Then define another variable named `keys` like this: | ||
```js | ||
const keys = Object.keys(car); | ||
``` | ||
|
||
Use `console.log()` to print the `keys` variable to the terminal. | ||
|
||
Check to see if your program is correct by running this command: | ||
|
||
```bash | ||
javascripting verify object-keys.js | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,11 @@ | ||
--- | ||
|
||
# | ||
# CORRECT. | ||
|
||
Good job using the Object.keys() prototype method. Remember to use it when you need to list the keys of an object. | ||
|
||
The next challenge is all about **functions**. | ||
|
||
Run `javascripting` in the console to choose the next challenge. | ||
|
||
--- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
const car = { | ||
make: 'Toyota', | ||
model: 'Camry', | ||
year: 2020 | ||
}; | ||
const keys = Object.keys(car); | ||
|
||
console.log(keys); |