if で条件分岐ができません

変数のスコープに注意してください。
2021年3月16日
ユーザー

いつもわかりやすい動画をありがとうございます。
さて、

    const dir = row === 2 ?
    Math.floor(Math.random() * 4) :
    Math.floor(Math.random() * 3) + 1;

の代わりに、

    if (row === 2) {
        const dir = Math.floor(Math.random() * 4);
    } else {
        const dir = Math.floor(Math.random() * 3) + 1;
    }

と書くと、dir が 定義されていない旨のエラーが出るのですが、どうしてでしょうか。

この回答を見るにはプレミアムサービスへの登録が必要です

プレミアムサービスとは?