Squares of the even numbers

Read a list of whole numbers, and print the squares of the even ones.

What to build

A short chain of array methods in place of a loop.

Requirements

  • Keep only the even numbers, in their original order.
  • Square each one.
  • Print them separated by spaces, or print none if there are no even numbers.

Input

One line of whole numbers separated by single spaces.

Output

One line.

Constraints

There are from 1 to 100 numbers, and they may be negative.

Example

Input:

1 2 3 4 5 6

Output:

4 16 36

Why it works

filter chooses and map changes, so each step does one job and the order of the items is preserved. An empty array joins to an empty string, which is why that case is checked before printing.

Examples

Example 1 · Input
1 2 3 4 5 6
Expected output
4 16 36
Example 2 · Input
7 9
Expected output
none

Hints

Hint 1

filter((x) => x % 2 === 0) keeps the even numbers.

Hint 2

map((x) => x * x) squares them.

Hint 3

join(" ") makes one line; check for an empty array first.

Solution

Show one correct solution (try it yourself first)
const nums = prompt().split(" ").map(Number);
const squares = nums.filter((x) => x % 2 === 0).map((x) => x * x);
console.log(squares.length ? squares.join(" ") : "none");

Lesson: Array methods: forEach, map, filter and reduce →