0
0
Javascriptprogramming~10 mins

Return inside loops in Javascript - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
Anumbers[0]
Bnumbers
Cnum % 2
Dnum
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.
2fill in blank
medium

Complete 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'
Aarr[i]
B-1
Ci
Darr
Attempts:
3 left
💡 Hint
Common Mistakes
Returning the value instead of the index.
Returning the whole array.
Returning -1 inside the loop.
3fill in blank
hard

Fix 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'
Astr.length
Bstr
Cstrings
Dstrings[0]
Attempts:
3 left
💡 Hint
Common Mistakes
Returning the length instead of the string.
Returning the whole array.
Returning a fixed element regardless of condition.
4fill in blank
hard

Fill 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'
Ai
Bj
Cnums.length
Dlength
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.
5fill in blank
hard

Fill 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'
Ai
B'a'
C2
D'A'
Attempts:
3 left
💡 Hint
Common Mistakes
Using uppercase 'A' instead of lowercase 'a'.
Using wrong loop variable name.
Comparing length with wrong number.