【问题标题】:Need help finding max value in java script (without max method)需要帮助在 javascript 中查找最大值(没有 max 方法)
【发布时间】:2021-06-19 06:58:34
【问题描述】:

首先我想说非常感谢您的光临。我是 Javascript 的初学者。现在处理一些问题,我需要帮助来确定我的台词出了什么问题。所以在这里,我想在函数中使用条件语句从变量中找出最大的数字,我现在卡住了。

已编辑:谢谢你们帮助我,现在我对我应该了解的更多内容有了清晰的认识。

const number1 = 103;
const number2 = 72;
const number3 = 189;

const getMax = (a, b, c) => {
  let max = a;
  
  if(b > max) {
    let max = b;
  } else if (c > max){
    let max = c;
  };
  
  return max;
}

getMax(number1, number2, number3);

console.log(`Max number is ${getMax}`);```

【问题讨论】:

  • else 应该做什么?
  • 和let也错了。
  • 你在哪里定义number?
  • 我在 getMax(number1, number2, number3); 处定义数字;

标签: javascript function conditional-statements


【解决方案1】:

我看到很多错误。 首先,您最多初始化 2 次。 let max = a; 然后你可以使用max。 但是else if也是错误的。

如果c < b < a 会怎样?然后只有第一个 if 适用,尽管 c 大于 a

【讨论】:

  • 您能否展示提问者需要使用的最终代码?
【解决方案2】:

你不需要省略else部分,因为如果b不大于a,你还需要和c比较。

另外两点是:

  1. 不需要声明一个分块的max,因为外部没有从条件中获取值。

  2. 你永远不会使用getMax的调用结果。

const getMax = (a, b, c) => {
    let max = a;

    if (b > max) max = b;
    if (c > max) max = c;

    return max;
}



console.log(`max value: ${getMax(2, 72, 189)}`);

【讨论】:

    【解决方案3】:

    使用let 关键字声明的变量具有块作用域,也就是说,它们只能在定义它们的块内访问。首先将max 设置为a,然后在if 语句中,您正在创建另一个同名的局部变量,并将其赋值为b。您应该做的是删除 if/else 语句中的 let 声明。

    其次,当满足第一个条件时,您的 if 块将被退出。您应该使用另一个 if 语句(无论第一个条件是否满足都会运行),而不是 else 语句(只有在第一个条件不满足时才会运行)。

    第三,您在 console.log(所有函数都是 javascript 中的对象)中传递了对函数的对象引用,这相当于函数文本。您需要改为传递函数调用。

    这就是你需要做的所有事情,除了不使用未定义的变量。

    const number1 = 54;
    const number2 = 72;
    const number3 = 189;
    
    const getMax = (a, b, c) => {
      let max = a;
      
      if(b > max) {
        max = b;
      }
      
      if (c > max){
        max = c;
      };
      
      return max;
    }
    
    console.log(`Nilai maksimum adalah ${getMax(number1, number2, number3)}`);

    【讨论】:

    • 非常感谢您的帮助,现在很清楚我应该了解更多信息
    【解决方案4】:

    正如 cmets 中指出的那样,您的代码存在一些问题,因此问题不是 100% 清楚,因此我向您介绍以下方法...

    function getMax() {
      const nums = arguments;
      var max = null;
    
      for (let i = 0; i < nums.length; i++) {
        const num = nums[i];
        if (max === null || num > max) {
          max = num;
        }
      }
    
      return max;
    }
    

    此函数接受 无限 个要比较的参数,并且不限于固定数量的输入。例如,以下都是有效的用途:

    const validExample1 = getMax(33, 72, 189, 4, 1); // Output: 189
    const validExample2 = getMax(2, 2); // Output: 2
    const validExample3 = getMax(320, 1, 8); // Output: 320
    const validExample4 = getMax(-1, -200, -34); // Output: -1
    

    通过不使用arrow function expression,您可以使用arguments 关键字来检索所有值的可迭代对象。或者,如果需要使用箭头函数,您可以简单地使用 spread syntax (...) do...

    const getMax = ( ...nums ) => {
      var max = null;
    
      for (let i = 0; i < nums.length; i++) {
        const num = nums[i];
        if (max === null || num > max) {
          max = num;
        }
      }
    
      return max;
    }
    

    完整的工作示例:

    const number1 = 3;
    const number2 = 72;
    const number3 = 189;
    
    function getMax() {
    
      const nums = arguments;
      var max = null;
    
      for (let i = 0; i < nums.length; i++) {
        const num = nums[i];
        if (max === null || num > max) {
          max = num;
        }
      }
    
      return max;
    }
    
    const result = getMax(number1, number2, number3);
    
    console.log(`Nilai maksimum adalah ${result}`);

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-22
      • 1970-01-01
      • 2011-03-14
      相关资源
      最近更新 更多