0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

10 行の出力 JavaScript編(paizaランク D 相当)

0
Last updated at Posted at 2026-07-31

本記事は、paizaラーニング「レベルアップ問題集」の学習記録です。
同コーナーでは、解答やコードの公開が認められています。

問題

次の10個の整数を、改行区切りで出力してください。

813
1
2
923874
23648
782356
3256
2342
24324
112

解答

10個の数値を配列にまとめ、join("\n") を使って改行区切りの文字列にします。

const numbers = [
  813,
  1,
  2,
  923874,
  23648,
  782356,
  3256,
  2342,
  24324,
  112,
];

console.log(numbers.join("\n"));

学んだこと

join() は、配列の要素を指定した文字でつなげて、1つの文字列にするメソッドです。

numbers.join("\n")

ここで使っている \n は、改行を表す特殊文字です。

そのため、配列の各要素が改行でつながり、次のように出力されます。

813
1
2
923874
23648
782356
3256
2342
24324
112

console.log() を10回書く方法でも出力できますが、配列と join("\n") を使うと、まとめてすっきり書けます。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?