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 element
for (let i = 0; i < arr.length; i++)3Is it even?
arr[i] % 2 === 0 ?4Yes → print it, No → skip
console.log(arr[i]);Explanation
- The
forloop visits every index from0toarr.length - 1. arr[i] % 2 === 0is 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.