Show the first element of an array of fruit names.
Acceptance criteria
- 1.#out should show Apple.
JavaScript Tutorial · JavaScript Arrays
These 10 tasks go with the JavaScript Array Access 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 array elements are read using their index, starting at 0.” .
1.Read the first elementexercise · easy · 10 min
Show the first element of an array of fruit names.
Passes when: #out should show Apple.
2.Read the last elementexercise · easy · 10 min · practice plan
Show the last element using the array length.
Passes when: #out should show Mango.
3.Use at with a negative indexexercise · easy · 10 min · practice plan
Show the second-to-last element using at().
Passes when: #out should show Banana.
4.Replace an elementexercise · easy · 10 min · practice plan
Replace the middle element with Cherry and show the whole list joined by commas.
Passes when: #out should show Apple,Cherry,Mango.
5.Read from a nested arrayexercise · medium · 10 min · practice plan
Show the value in the second row, first column of a grid.
Passes when: #out should show 30.
6.Fix the off-by-one first elementbug fix · easy · 8 min
Counting starts at 0, not 1.
Passes when: #out should show Apple.
7.Fix the last element past the endbug fix · easy · 8 min · practice plan
Using length as an index reads one place too far.
Passes when: #out should show Mango.
8.Fix the index in quotesbug fix · easy · 8 min · practice plan
A string is treated as a property name, not a position.
Passes when: #out should show Banana.
9.Fix at used with a positive onebug fix · easy · 8 min · practice plan
at(1) is the second element, not the last.
Passes when: #out should show Mango.
10.Fix the single bracket on a gridbug fix · easy · 8 min · practice plan
One pair of brackets gives a whole row, not a value.
Passes when: #out should show 30.
Between them these tasks cover Indexes, at() and Nested access.
Looking for a different chapter? See every practice set, or switch to the interview theory questions.