Show the index of Banana in a list of fruit.
Acceptance criteria
- 1.#out should show 1.
JavaScript Tutorial · JavaScript Arrays
These 10 tasks go with the JavaScript Array Search 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 indexOf and includes tell you whether a value is in an array and where it sits.” .
1.Find a positionexercise · easy · 10 min
Show the index of Banana in a list of fruit.
Passes when: #out should show 1.
2.Check for a missing valueexercise · easy · 10 min · practice plan
Use includes to check for a fruit that is not in the list.
Passes when: #out should show false.
3.Report a missing indexexercise · easy · 10 min · practice plan
Show the value indexOf gives back when nothing matches.
Passes when: #out should show -1.
4.Find the last matchexercise · easy · 10 min · practice plan
Show the index of the last time a value appears.
Passes when: #out should show 3.
5.Search from a positionexercise · medium · 10 min · practice plan
Find the next match after index 0 in a list with two matches.
Passes when: #out should show 2.
6.Fix the truthy index checkbug fix · easy · 8 min
An index of 0 is falsy, so a match at the front is reported as missing.
Passes when: #out should show found it.
7.Fix the wrong missing valuebug fix · easy · 8 min · practice plan
indexOf returns -1 when nothing matches, not undefined.
Passes when: #out should show -1.
8.Fix the type mismatchbug fix · easy · 8 min · practice plan
The list holds numbers, but a string is being searched for.
Passes when: #out should show true.
9.Fix first used instead of lastbug fix · easy · 8 min · practice plan
The last matching position is wanted, not the first.
Passes when: #out should show 3.
10.Fix the object compared by valuebug fix · easy · 8 min · practice plan
Two objects that look alike are still different objects.
Passes when: #out should show true.
Between them these tasks cover indexOf, includes and lastIndexOf.
Looking for a different chapter? See every practice set, or switch to the interview theory questions.