Read the question, write JavaScript on the right, then Run or Check.
QuestionHint and solution stay closed until you open them
Read n and log the squares of 1 through n on a single line, separated by spaces.
Build the values into an array, then join them with a space.
Input. One line: an integer n ≥ 1.
Output. Space-separated squares, e.g. 1 4 9 16 25 for n = 5.
Constraints
- 1 ≤ n ≤ 20
Examples
Input
5
Output
1 4 9 16 25
Hint
- Push i * i into an array inside the loop.
- arr.join(" ") turns [1,4,9] into "1 4 9".
Show correct code
Peek only after you have tried. You can still Check your own version.
const n = Number(readLine());
const out = [];
for (let i = 1; i <= n; i++) out.push(i * i);
console.log(out.join(" "));
main.jsconsole.log · readLine() · Ctrl + Enter
ResultIdle
Run to see output. Check grades the tests.