Back to Interview Prep

Warm Up · Loops 01

Print All Even Numbers in an Array

Problem Statement

Write a program to print all even numbers from an array.

Example

Input: [10, 3, 5, 2, 7, 6, 9]

Process: Keep only elements where element % 2 === 0

Output: 10 2 6

Approach

  • Iterate through each element in the array.
  • Check if the element is divisible by 2.
  • If yes, print the element (it's even).

Visualisation

1Start with the array[10, 3, 5, 2, 7, 6, 9]
2Visit each elementfor (let i = 0; i < arr.length; i++)
3Is it even?arr[i] % 2 === 0 ?
4Yes → print it, No → skipconsole.log(arr[i]);

Explanation

  • The for loop visits every index from 0 to arr.length - 1.
  • arr[i] % 2 === 0 is true only for even numbers.
  • Even numbers are printed; odd numbers are skipped and the loop moves on.
  • For [10, 3, 5, 2, 7, 6, 9] this prints 10, 2 and 6.

Your solution

Solve it in JavaScript and print the answer with console.log(). Run to see your output, then Submit to check it against the test cases.

// Write your solution in JavaScript
// Print the answer with console.log()


Console

No output yet. Use console.log() to print values here.

Write your own solution and press Submit once to unlock the answer.

You're viewing a free lesson. Sign up free to track your progress and unlock the full course.Sign up freeSign in