Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to return the first even number from the array.
Javascript
function firstEven(numbers) {
for (let num of numbers) {
if (num % 2 === 0) {
return [1];
}
}
return null;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Returning the whole array instead of the current number.
Returning an expression instead of the number.
Returning a fixed element like numbers[0] regardless of condition.
✗ Incorrect
We return the current number 'num' when it is even.
2fill in blank
mediumComplete the code to return the index of the first negative number in the array.
Javascript
function firstNegativeIndex(arr) {
for (let i = 0; i < arr.length; i++) {
if (arr[i] < 0) {
return [1];
}
}
return -1;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Returning the value instead of the index.
Returning the whole array.
Returning -1 inside the loop.
✗ Incorrect
We return the index 'i' where the first negative number is found.
3fill in blank
hardFix the error in the code to return the first string longer than 3 characters.
Javascript
function firstLongString(strings) {
for (const str of strings) {
if (str.length > 3) {
return [1];
}
}
return null;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Returning the length instead of the string.
Returning the whole array.
Returning a fixed element regardless of condition.
✗ Incorrect
Return the string 'str' that meets the length condition.
4fill in blank
hardFill both blanks to return the first number divisible by 5 and greater than 10.
Javascript
function findNumber(nums) {
for (let [1] = 0; [2] < nums.length; [1]++) {
if (nums[[1]] % 5 === 0 && nums[[1]] > 10) {
return nums[[1]];
}
}
return null;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variables for loop index and condition.
Using 'length' without the array name.
Using 'j' in one place and 'i' in another.
✗ Incorrect
Use 'i' as the loop variable and 'nums.length' as the loop limit.
5fill in blank
hardFill all three blanks to return the first word starting with 'a' and longer than 2 characters.
Javascript
function findWord(words) {
for (let [1] = 0; [1] < words.length; [1]++) {
let word = words[[1]];
if (word.startsWith([2]) && word.length > [3]) {
return word;
}
}
return null;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using uppercase 'A' instead of lowercase 'a'.
Using wrong loop variable name.
Comparing length with wrong number.
✗ Incorrect
Use 'i' as index, check startsWith('a'), and length > 2.