Skip to content

20240905 FizzBuzz問題 #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Oct 4, 2024
Merged

20240905 FizzBuzz問題 #1

merged 4 commits into from
Oct 4, 2024

Conversation

taichihub
Copy link
Owner

@taichihub taichihub commented Sep 5, 2024

プラクティス

FizzBuzz問題(JavaScript)

FizzBuzz実行結果

chiroru@MacBook-Pro-3 01.fizzbuzz % ./fizzbuzz.js
1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
FizzBuzz
16
17
Fizz
19
Buzz

フォーマット

下記2コマンドで整形

npm run lint
npm run fix
chiroru@MacBook-Pro-3 01.fizzbuzz % npm run lint
npm run fix


> lint
> prettier --check . && eslint .

Checking formatting...
[warn] fizzbuzz.js
[warn] Code style issues found in the above file. Run Prettier with --write to fix.

> fix
> prettier --write . && eslint --fix .

eslint.config.js 60ms (unchanged)
fizzbuzz.js 17ms
package-lock.json 44ms (unchanged)
package.json 3ms (unchanged)
prettier.config.js 2ms (unchanged)

参考記事

等価演算子と厳密等価演算子の違い

@taichihub taichihub changed the title FizzBuz問題 F20240905 izzBuzz問題 Sep 5, 2024
@taichihub taichihub changed the title F20240905 izzBuzz問題 20240905 FizzBuzz問題 Sep 5, 2024
output += "Buzz";
}
if (output === "") {
output = i;
Copy link

@cafedomancer cafedomancer Sep 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

他の行では output に string が格納されますが、この行では output に number が格納されます。特定の変数にはある 1 つのデータ型のデータを格納する方がよいので、そうしてくださいー。

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cafedomancer
かしこまりました。変数展開を使用してoutputに入るデータ型を文字列に統一しました。

  if (output === '') {
    output = `${i}`;
  }

output += "Buzz";
}
if (output === "") {
output = `${i}`;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

number を string に変換したいのであれば別の方法を使ってください。テンプレート文字列は、文字列中に変数を埋め込むための仕組みだからです。

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cafedomancer

テンプレート文字列は、文字列中に変数を埋め込むための仕組みだからです。

かしこまりました。以後、テンプレート文字列を使用する際は今回のように単体で使用するのではなく、文字列の中で必要な際に使用するようにします。

toStringメソッドを使用して実装するようにしました。

  if (output === "") {
    output = i.toString();
  }

@taichihub
Copy link
Owner Author

@cafedomancer
アプルーブを頂いたのでマージします。

@taichihub taichihub merged commit 7b30787 into main Oct 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants