不惑にしてまだ何者でもない者のブログ

Arduino関連、Raspberry Pi関連、プログラミング学習

paizaラーニング『JavaScript入門編2: 条件によって処理を変えてみよう (全6回) 』を受講してみた

2020-10-18 13:28:20 | paiza
次は、『JavaScript入門編2: 条件によって処理を変えてみよう (全6回) 』を受講。


01:数値が一致した場合、メッセージを表示

  • if文の基本形
// if文による条件分岐
process.stdin.resume();
process.stdin.setEncoding('utf8');
var number = 1;
if (number == 1) {
 console.log("スキ!"); // 条件式が成立したときの処理
} else {
 console.log("キライ"); // 条件式が成立しなかったときの処理
}

02:複数の条件を組み合わせてみよう

  • if - else if -else  の基本形
// if文による条件分岐
process.stdin.resume();
process.stdin.setEncoding('utf8');
var number = 1;
if (number == 1) {
 console.log("スキ!"); // 条件式が成立したときの処理
} else if (number == 2) {
 console.log("どちらでもない");// 条件式2が成立したときの処理
} else {
 console.log("キライ"); // 条件式が成立しなかったときの処理
}

03:比較演算子で条件分岐してみよう

  • 比較演算子の種類
    • a==b:aとbが等しい
    • a
    • a>b:aがbよりも大きい
    • a<=b:aがb以下である
    • a>=b:aがb以上である
    • a!=b:aとbが等しくない

04:おみくじを作ってみよう

  • おみくじ
// おみくじを作る
// 比較演算子 == > < >= <= !=
// 大吉 中吉 小吉 凶 大凶
process.stdin.resume();
process.stdin.setEncoding('utf8');
var omikuji = parseInt(Math.random() * 10) + 1;
console.log(omikuji);
if (omikuji == 1) {
 console.log("大吉");
} else if (omikuji ==2) {
 console.log("中吉");
} else if (omikuji <= 4) {
 console.log("小吉");
} else if (omikuji <= 7) {
 console.log("凶");
} else {
 console.log("大凶");
}

05:RPGのクリティカルヒットを再現

  • おみくじ
// RPGのクリティカルヒットを再現
// 比較演算子 == > < >= <= !=

// スライムと戦っている。
// 1から10の目のサイコロをふって、
// 6未満:サイコロの目だけダメージを与えたと表示。
// 6以上:クリティカルヒットとして、100のダメージを与えたと表示。

process.stdin.resume();
process.stdin.setEncoding('utf8');
var hit = parseInt(Math.random() * 10) + 1;
console.log(hit);
if (hit
 console.log("スライムに、" + hit + "のダメージを与えた!");
} else {
 console.log("クリティカルヒット!スライムに、100のダメージを与えた!");
}

06:西暦から平成何年か求めてみよう

  • 平成年から西暦年を求める
    • 平成年に1988を足す
  • 西暦年から平成年を求める
    • 西暦から1988を引く

感想

ちょっと冗長だったかな。
西暦から平成への変換って。
もう時代は令和だけどな😁 


最新の画像もっと見る

コメントを投稿