【问题标题】:Udacity: Quiz: Navigating the Food Chain - ternary operator within ternary operatorUdacity:测验:导航食物链 - 三元运算符中的三元运算符
【发布时间】:2018-12-31 01:46:47
【问题描述】:

我目前正在学习关于 Javascript 的 Udacity 课程,一年后我又重新学习了该课程。我回到了我知道答案的地方,但我似乎无法将其反向工程为简单的if 声明

路线:

编写一系列将变量类别设置为等于的三元语句:

“草食动物”如果动物吃植物 “食肉动物”如果动物吃动物 “杂食动物”如果动物吃植物和动物 如果动物不吃植物或动物,则未定义

使用eatsPlantseatsAnimals 变量来测试您的代码。

工作答案:

var eatsPlants = false;
var eatsAnimals = true;

var category = (eatsPlants) ? (eatsAnimals) ? "omnivore" : "herbivore" 
: (eatsAnimals) ? "carnivore" : undefined;

console.log(category);

我想了解为什么会有 (eatsPlants) ? (吃动物)? 为什么 (eatsAnimals) 被使用了两次?或者简单地说,有人可以为我分解吗?我对编码还是很陌生。

我能想到的最好的就是这个,但它并没有帮助我理解三元版本:

var eatsPlants = false;
var eatsAnimals = true;

var category;

 if (eatsPlants && eatsAnimals)
 {
    category = "Omnivore"; 

 }  else if (eatsAnimals){
     category="Carnivore"

 } else if (eatsPlants) {
     category = "Herbivore";
 }
 else
 {
     category= undefined;
 }

 console.log(category);

提前感谢您的所有帮助

【问题讨论】:

    标签: javascript


    【解决方案1】:

    这是一段令人困惑的代码。更合乎逻辑地缩进,它看起来像:

    var category = (eatsPlants)
      ? (eatsAnimals ? "omnivore" : "herbivore")
      : (eatsAnimals ? "carnivore" : undefined);
    

    如果eatsPlants 为真,则需要根据eatsAnimals 条件在“杂食动物”和“草食动物”之间做出决定。否则,如果eatsPlants 为假,则需要在“食肉动物”和undefined 之间做出决定——同样基于eatsAnimals 条件。

    因此,来自eatsPlants 的每个可能路径都需要检查eatsAnimals 条件以确定要解析到哪个字符串。

    if/else 版本看起来像:

    var category;
    if (eatsPlants) {
      if (eatsAnimals) {
        category = 'omnivore';
      } else {
        category = 'herbivore';
      }
    } else {
      // eatsPlants is false:
      if (eatsAnimals) {
        category = 'carnivore';
      } else {
        category = undefined;
      }
    }
    

    如您所见,通过使用与三元运算符版本相同的逻辑,它也必须检查两次eatsAnimals

    我不建议在这里使用三元运算符 - 本课程的问题是要求您编写看起来非常混乱的代码。这不是一种记忆的技巧。我更喜欢使用数组,所以代码阅读器可以看到网格:

    const categories = [
      // eatsAnimals is
      //   0            1
      [undefined, 'Carnivore'],   // eatsPlants is 0
      ['Herbivore', 'Omnivore']   // eatsPlants is 1
    ]
    
    var eatsPlants = 0;
    var eatsAnimals = 1;
    console.log(categories[eatsPlants][eatsAnimals]);

    【讨论】:

      【解决方案2】:

      首先,不要气馁,继续学习编程。
      像这样的东西,尤其是它的格式,会让每个人感到困惑:

      var category = (eatsPlants) ? (eatsAnimals) ? "omnivore" : "herbivore" 
      : (eatsAnimals) ? "carnivore" : undefined;
      

      在查看这种冗长而复杂的三元语句组合时始终牢记三元语句的基本语法

      (condition) ? expression if true : expression if false
      

      我已重新格式化代码并添加了 cmets 以使其更易于阅读:

      var category =
          (eatsPlants) ?
          (eatsAnimals) ?   // eatsPlants  === true
          "omnivore" :      // eatsPlants  === true  && eatsAnimals === true
          "herbivore" :     // eatsPlants  === true  && eatsAnimals === false
          (eatsAnimals) ?   // eatsPlants  === false
          "carnivore" :     // eatsAnimals === true
          undefined;        // eatsPlants  === false && eatsAnimals === false
      

      如果为了清楚起见,我们使用单独的 if 语句重写逻辑,我们会得到以下结果:

      if (eatsPlants === true && eatsAnimals === true) {
          category = 'omnivore';
      }
      if (eatsPlants === true && eatsAnimals === false)
          category = 'herbivore';
      }
      if (eatsPlants === false && eatsAnimals === true)
          category = 'carnivore';
      }
      if (eatsPlants === false && eatsAnimals === false)
          category = 'undefined';
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-10-06
        • 2018-09-10
        • 2016-01-15
        • 2021-08-01
        相关资源
        最近更新 更多