Pull the name and age out of a person object into their own variables.
Acceptance criteria
- 1.#out should show Ada is 36.
JavaScript Tutorial · JavaScript Objects
These 10 tasks go with the JavaScript Destructuring chapter. 5 of them ask you to build something small from scratch; 5 give you code that is already broken and ask you to work out why. Set aside about 90 minutes for the whole set, though you can do them in any order.
Nothing here is marked by a person. When you press Submit, the editor checks your code against the points listed under each task and tells you straight away what passed and what didn't (you need a free account to submit). If you get stuck, there are hints, and a worked solution once you've had a go. The first 2 tasks are free; the rest are part of the practice plan.
If it's been a while since you read the chapter, here is how it starts: “JavaScript destructuring pulls values out of objects and arrays into their own variables.” .
1.Destructure an objectexercise · easy · 10 min
Pull the name and age out of a person object into their own variables.
Passes when: #out should show Ada is 36.
2.Destructure an arrayexercise · easy · 10 min · practice plan
Pull the first two colours out of an array.
Passes when: #out should show red green.
3.Skip an elementexercise · easy · 10 min · practice plan
Use a gap to take only the third colour.
Passes when: #out should show blue.
4.Rename while destructuringexercise · easy · 10 min · practice plan
Pull out name but call the variable fullName.
Passes when: #out should show Grace Hopper.
5.Use a default valueexercise · medium · 10 min · practice plan
Give size a default because the object does not have one.
Passes when: #out should show dark medium.
6.Fix the brackets used for an objectbug fix · easy · 8 min
Objects destructure by name with braces, so position gives the wrong value.
Passes when: #out should show Ada.
7.Fix the name that does not matchbug fix · easy · 8 min · practice plan
Object destructuring matches by property name.
Passes when: #out should show Ada.
8.Fix the default that never appliesbug fix · easy · 8 min · practice plan
A default only fills in for undefined, and the value was set to null.
Passes when: #out should show medium.
9.Fix the missing gapbug fix · easy · 8 min · practice plan
Without the gap the first element is taken instead of the third.
Passes when: #out should show blue.
10.Fix the parameter not destructuredbug fix · easy · 8 min · practice plan
The function receives an object but reads it as if it were a plain value.
Passes when: #out should show Ada from London.
Between them these tasks cover Destructuring, Renaming and Default values.
Looking for a different chapter? See every practice set, or switch to the interview theory questions.