Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to find the first occurrence index of a target element in an array.
DSA Javascript
function firstOccurrence(arr, target) {
for (let i = 0; i < arr.length; i++) {
if (arr[i] === [1]) {
return i;
}
}
return -1;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong variable for comparison like 'arr' or 'i'.
Returning the element instead of the index.
✗ Incorrect
We compare each element arr[i] with the target to find the first occurrence index.
2fill in blank
mediumComplete the code to find the last occurrence index of a target element in an array.
DSA Javascript
function lastOccurrence(arr, target) {
for (let i = arr.length - 1; i >= 0; i--) {
if (arr[i] === [1]) {
return i;
}
}
return -1;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong variable for comparison.
Looping forward instead of backward.
✗ Incorrect
We compare each element arr[i] with the target to find the last occurrence index starting from the end.
3fill in blank
hardFix the error in the code to correctly find the first occurrence of target in the array.
DSA Javascript
function firstOccurrence(arr, target) {
let index = -1;
for (let i = 0; i < arr.length; i++) {
if (arr[i] === [1]) {
index = i;
break;
}
}
return index;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using assignment '=' instead of comparison '==='.
Putting the wrong variable on the right side.
✗ Incorrect
The comparison should be arr[i] === target, but the blank replaces the right side of the comparison, so 'target' is correct.
4fill in blank
hardFill both blanks to create a function that returns an object with first and last occurrence indices of target in arr.
DSA Javascript
function firstAndLastOccurrence(arr, target) {
let first = -1;
let last = -1;
for (let i = 0; i < arr.length; i++) {
if (arr[i] === [1]) {
if (first === -1) {
first = i;
}
last = [2];
}
}
return { first, last };
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Assigning last to first instead of current index.
Comparing arr[i] to wrong variable.
✗ Incorrect
We compare arr[i] to target and update last with the current index i.
5fill in blank
hardFill all three blanks to create a function that returns an object with first and last occurrence indices of target using a single pass.
DSA Javascript
function findOccurrences(arr, target) {
let result = { first: -1, last: -1 };
for (let i = 0; i < [1]; i++) {
if (arr[i] === [2]) {
if (result.first === -1) {
result.first = i;
}
result.last = [3];
}
}
return result;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong loop limit.
Comparing with wrong variable.
Assigning last to wrong value.
✗ Incorrect
Loop runs till arr.length, compare arr[i] with target, update last with current index i.