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?

More than 3 years have passed since last update.

【剰余演算子】%ってなんだっけ?編

Last updated at Posted at 2020-12-07

読んでなるほど!と思うのに、しばらくするとなんだっけ?ってなるシリーズその2

本日読んでいて忘れていた部分を抜粋。
剰余演算子は馴染み深い「 +-*/ 」以外に「 % 」というのがいることを忘れていました。

number.js
const num = 60

if (num % 15 == 0) {
  console.log(`${num}は3と5の倍数です`)
} else if (num % 3 == 0) {
  console.log(`${num}は3の倍数です`)
} else if (num % 5 == 0) {
  console.log(`${num}は5の倍数です`)
} else {
  console.log(`${num}は3の倍数でも、5の倍数でもありません`)
}

const num = 60
だから
60 % 15 == 0
ということ

ただの割り算は「 / 」なので考え方が違う
「 % 」は割り算した答えの数字じゃなくて、その答えの余りのこと
60 / 15 = 4
4で割り切れてるから余りは0

つまり60 % 15 == 0は、 0 == 0だよね?ということで→true

 ⬇️出力結果⬇️
60は3と5の倍数です

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?