論理演算子 パート3
CoddyのJavaScriptジャーニー「基礎」セクションの一部。レッスン 21/77。
論理式を扱う際、時にはそれらを簡略化したり再配置したりする必要があります。
! (not) が && (and) で結合された two つの conditions の前にある場合、それを2つの独立した部分に分割できます。&& (and) は || (or) になり、each パートにはそれぞれの ! (not) が付きます:
!(A && B) は (!A) || (!B) と同じです
例えば:
// 数値が1から10の間にないか確認しましょう
let number = 15
// これら2つの式は等価です:
let result1 = !(number >= 1 && number <= 10)
let result2 = !(number >= 1) || !(number <= 10)
console.log(result1) // True
console.log(result2) // True逆もまた正しいです:!(A || B) は (!A) && (!B) と同じです
例えば、以下のようになります:
// 人が学生でも雇用されてもいないかを確認
let is_student = false
let is_employed = false
// これら2つの式は等価です:
let result1 = !(is_student || is_employed)
let result2 = !is_student && !is_employed
console.log(result1) // True
console.log(result2) // Trueチャレンジ
初心者ペットショップが顧客にペットを販売できるかどうかを判定するシステムの作成を手助けしています。
以下の variables を Initialize してください:
has_licenseに値truehas_spaceに値falsehas_experienceに値true
以下を判定するための logical expressions を記述してください:
can_sell_regular_pet:顧客にはライセンスまたは経験のいずれかが必要で、かつスペースが必須であるcan_sell_exotic_pet:顧客にはライセンスと経験の両方が必要で、かつスペースが必須であるcannot_sell_any_pet:顧客にはライセンスも経験もない、またはスペースがない
自分で試してみよう
// 変数を初期化する
// タスクに記載されている3つの変数をここで宣言する
// 条件を計算する
// 以下の各プレースホルダーを、タスクで説明されている論理式に置き換える
let can_sell_regular_pet = false
let can_sell_exotic_pet = false
let cannot_sell_any_pet = false
// 以下の行を削除しないでください
console.log("Can sell regular pet:", can_sell_regular_pet)
console.log("Can sell exotic pet:", can_sell_exotic_pet)
console.log("Cannot sell any pet:", cannot_sell_any_pet)このレッスンには短いクイズがあります。レッスンを始めて解答し、進捗を記録しましょう。
基礎のすべてのレッスン
自分で練習してみよう: JavaScriptオンラインコンパイラ