JavaScript bootcamp · Lab 30

Keep the evens

easyArrays10 minLesson: Array Methods

Read the question, write JavaScript on the right, then Run or Check.

QuestionHint and solution stay closed until you open them

Read a line of integers. Log the even ones space-separated (same order).

Input. Space-separated integers.

Output. Evens space-separated.

Examples

Example 1
Input
1 2 3 4 5
Output
2 4
Example 2
Input
1 3 5
Output
Hint
  1. filter(x => x % 2 === 0).join(' ')
Show correct code

Peek only after you have tried. You can still Check your own version.

const nums = readLine().trim().split(/\s+/).filter(Boolean).map(Number);
console.log(nums.filter((x) => x % 2 === 0).join(' '));
main.jsconsole.log · readLine() · Ctrl + Enter
ResultIdle
Run to see output. Check grades the tests.