【问题标题】:Code working with ternary operator but not with if else statements使用三元运算符但不使用 if else 语句的代码
【发布时间】:2019-09-01 14:56:42
【问题描述】:

这个 javascript 程序可以按预期使用三元运算符,但不能使用 if else 语句。我做错了什么?

我正在尝试解决一些基本的 javascript 练习,但我被困在这个问题上。 https://www.w3resource.com/javascript-exercises/javascript-basic-exercise-74.php

//Working code with ternary operator
    function all_max(nums) {
      var max_val = nums[0] > nums[2] ? nums[0] : nums[2];

      nums[0] = max_val;
      nums[1] = max_val;
      nums[2] = max_val;

      return nums;
      }
    console.log(all_max([20, 30, 40]));
    console.log(all_max([-7, -9, 0]));
    console.log(all_max([12, 10, 3]));

// 使用 if-else 语句

  function all_max(nums) {
     if (var max_val = nums[0] > nums[2]) {
     return nums[0];
    } else {
     return nums[2];
  }

     nums[0] = max_value ;
     nums[1] = max_value ;
     nums[2] = max_value ;

return nums;
}
console.log(all_max([20, 30, 40]));
console.log(all_max([-7, -9, 0]));
console.log(all_max([12, 10, 3]));

【问题讨论】:

  • 第二个版本中有return 语句。 return 语句立即退出函数。
  • variable assignment inside if it statement 太丑了
  • @Pointy 即使在删除 return 语句后也不走运
  • @CodeManiac 好点。将其移到 if 语句之外

标签: javascript ecmascript-6 ternary-operator


【解决方案1】:

您应该在 if/else 语句的主体中分配值,而不是在比较中,所以这样的事情应该适合您:

function all_max(nums) {
  let max_val = 0
  if (nums[0] > nums[2]) {
    max_val = nums[0];
  } else {
    max_val = nums[2];
  }
  nums[0] = max_val;
  nums[1] = max_val;
  nums[2] = max_val;

  return nums;
}

console.log(all_max([20, 30, 40]));
console.log(all_max([-7, -9, 0]));
console.log(all_max([12, 10, 3]));

【讨论】:

    【解决方案2】:

    以下代码有效

          function all_max(nums) {
    let max_val = nums[0] > nums[2]
         if (max_val) {
         return nums[0];
        } else {
         return nums[2];
      }
    
         nums[0] = max_value ;
         nums[1] = max_value ;
         nums[2] = max_value ;
    
    return nums;
    }
    console.log(all_max([20, 30, 40]));
    console.log(all_max([-7, -9, 0]));
    console.log(all_max([12, 10, 3]));
    

    在 if 条件外计算 max_val 并将结果放入 if 条件 让 max_val = nums[0] > nums[2]

    【讨论】:

    • 检查max_value变量是否未定义
    【解决方案3】:

    不允许在if 语句中声明变量。删除它。

    如果你只想要最大值试试这个

     function all_max(nums) {
       if (nums[0] > nums[2]) {
             max_value =  nums[0];
       } else {
             max_value = nums[2];
       }
       return max_value;
     } 
     console.log(all_max([20, 30, 40]));
     console.log(all_max([-7, -9, 0]));
     console.log(all_max([12, 10, 3]));

    如果要将数组中的所有元素设置为最大值,则使用此

    function all_max(nums) {
         if (nums[0] > nums[2]) {
                 max_value =  nums[0];
         } else {
                 max_value = nums[2];
         }
         nums[0] = max_value;
         nums[1] = max_value;
         nums[2] = max_value;
         return nums;
    }
    console.log(all_max([20, 30, 40]));
    console.log(all_max([-7, -9, 0]));
    console.log(all_max([12, 10, 3]));

    【讨论】:

      猜你喜欢
      • 2017-11-19
      • 1970-01-01
      • 2021-07-09
      • 1970-01-01
      • 1970-01-01
      • 2021-03-31
      • 2021-09-15
      • 2021-09-10
      • 1970-01-01
      相关资源
      最近更新 更多