Recall & Review
beginner
What does 'First Occurrence of Element' mean in an array?
It means the position (index) where the element appears for the very first time when we look from the start of the array.
Click to reveal answer
beginner
What does 'Last Occurrence of Element' mean in an array?
It means the position (index) where the element appears for the very last time when we look from the end of the array.
Click to reveal answer
intermediate
Why is it useful to find the first and last occurrence of an element?
It helps us know the range where the element appears in the array, which is useful for counting how many times it appears or for searching efficiently.
Click to reveal answer
beginner
What is a simple way to find the first occurrence of an element in JavaScript?
Use the array method
indexOf(element), which returns the first index where the element is found or -1 if not found.Click to reveal answer
beginner
How can you find the last occurrence of an element in JavaScript?
Use the array method
lastIndexOf(element), which returns the last index where the element is found or -1 if not found.Click to reveal answer
Which JavaScript method returns the first index of an element in an array?
✗ Incorrect
The method indexOf() returns the first index where the element is found.
What does lastIndexOf() return if the element is not found in the array?
✗ Incorrect
lastIndexOf() returns -1 when the element is not found.
If an element appears multiple times, what does indexOf() return?
✗ Incorrect
indexOf() returns the first position where the element appears.
Which method would you use to find the last occurrence of an element in an array?
✗ Incorrect
lastIndexOf() returns the last position of the element in the array.
What is the output of ['a', 'b', 'a', 'c'].indexOf('a')?
✗ Incorrect
The first 'a' is at index 0.
Explain how to find the first and last occurrence of an element in a JavaScript array.
Think about built-in array methods that give positions.
You got /3 concepts.
Why might knowing the first and last occurrence of an element be helpful in programming?
Consider how knowing positions helps with array operations.
You got /3 concepts.