25
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

「進捗・どう・です・か」をランダムに表示し「進捗どうですか」が完成したら煽ってくるプログラムをES6で書いてみる

Last updated at Posted at 2017-09-03

ちょっとネタが古いですが面白そうだったのでコーディングしたいと思います。

プログラム

const words = ['進捗', 'どう', 'です', ''];

let result = '';
while (!result.endsWith(words.join(''))) {
  const ramdomIndex = Math.floor(Math.random() * words.length);
  result += words[ramdomIndex];
}

console.log(`${result}???`);
console.log(`
_人人人人人人人_
>進捗どうですか<
 ̄Y^Y^Y^Y^Y^Y^Y ̄`);
console.log(`${result.length}文字で煽られました。`);
Console


ですどう進捗か進捗どうかかどうかかですかかかかどうどうかですどう進捗どうどうどうどう進捗かどう進捗どうどう進捗どうかかかかどうかかですですですです進捗ですかかかかです進捗進捗どうか進捗進捗どうですですですどうですかどうですかどう進捗ですですどうか進捗進捗進捗進捗どうかかどうです進捗かですか進捗進捗進捗か進捗どう進捗です進捗ですです進捗どう進捗進捗かですですどうですですかどうですかかどうかですです進捗です進捗です進捗です進捗進捗進捗進捗かどうか進捗どうどうどうです進捗かどうかですです進捗進捗進捗どうどうどう進捗進捗進捗ですですかどうどうですどうどうです進捗か進捗かかどうどうですどうどう進捗どうか進捗か進捗か進捗どうかか進捗か進捗かかどうどうかかどうどうかどうです進捗です進捗どうですか???

_人人人人人人人_
>進捗どうですか<
 ̄Y^Y^Y^Y^Y^Y^Y ̄

 345文字で煽られました。

String.prototype.endsWith()

.endsWith() は対象の文字列の末尾に引数の文字列が存在するかを真偽値で返します。
Array.prototype.join() は引数の文字列で配列を結合した文字列を返します。
プログラムでは result'進捗どうですか'で終了するかを判定し終わらない限りループを続けます。
result.indexOf(words.join('')) >= 0で同様のことができますが、.endsWith()
を使ったほうが何をしたいのかが明確になります。

array[Math.floor(Math.random() * array.length]

ES6ではありませんが、配列にランダムなインデックスで要素にアクセスするイディオムです。

テンプレート文字列

**``(バッククォート)**で囲む文字列リテラルです。
\n を書かなくても改行をそのまま認識します。
**${}(プレースホルダー)**に変数・関数をそのまま埋め込むことができます。
(以前のように '+' で結合する必要がありません。)

ここまで読んでいただきありがとうございました。

25
10
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
25
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?