javascript Javascript从3个输入中找到最大数量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26920610/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 06:41:14  来源:igfitidea点击:

Javascript find max number from 3 inputs

javascriptmax

提问by Pizzaman

I've just began Javascript in college. One task is to define and call a function to find the maximum number from 3 numbers entered by the user. I know there is a max() function but we were told to do it manually using if and else if.

我刚刚在大学开始学习 Javascript。一项任务是定义并调用一个函数,以从用户输入的 3 个数字中找到最大数字。我知道有一个 max() 函数,但我们被告知使用 if 和 else if 手动完成。

I'm going wrong somewhere as you can see. It's just telling me the max is 0 everytime.

正如你所看到的,我在某个地方出错了。它只是告诉我每次最大值为 0。

function maxNum(num1, num2, num3){
        var max = 0;
        if(num1 > num2){
            if(num1 > num3){
                num1 = max;
            }
            else{
                num3 = max;
            }
        }
        else{
            if(num2 > num3){
                num2 = max;
            }
        }
    return max;
    }

    for(i=0;i<3;i++){
        parseInt(prompt("Enter a number"));
    }
    document.write(maxNum());

采纳答案by Bijan

One problem you have is that you do not save the number the user inputs. You prompt them, parse it as an int and then nothing. You have to pass the 3 numbers into maxNum()

您遇到的一个问题是您没有保存用户输入的数字。您提示他们,将其解析为 int,然后什么也不做。您必须将 3 个数字传入maxNum()

Here is a working example that uses proper left hand assignment and saves the number. Also it is a good idea to use >=instead of >because the user can enter 2 of the same number

这是一个使用正确左手分配并保存数字的工作示例。使用>=代替也是一个好主意,>因为用户可以输入 2 个相同的数字

function maxNum(num1, num2, num3){
        var max = 0;
        if((num1 >= num2) && (num1 >= num3)){
            max = num1;
        }
        else if((num2 >= num1) && (num2 >= num3)){
            max = num2;
        }
        else{
            max = num3;
        }
    return max;
    }

    var arr = []; 
    for(i=0;i<3;i++){
        arr[i] = parseInt(prompt("Enter a number"));
    }


    document.write(maxNum.apply(this, arr));

回答by Suresh KUMAR Mukhiya

Or you can use ES6 syntax, to compute largest of three numbers in easier way,

或者您可以使用 ES6 语法,以更简单的方式计算三个数字中最大的一个,

const largest = a => F = b => G = c => ((a > b && a > c) ? a : (b > a && b > c) ? b : c)

console.time()
console.log(largest(53)(30907)(23333))
console.timeEnd()

回答by ariel_556

easiest way:

最简单的方法:

function maxNum(num1, num2, num3){
    var tmp = 0;
    if(num1 < num2 && num3 < num2) {
        tmp = num2;
    } else if(num3 < num1){
        tmp = num1;
    } else {
        tmp = num3;    
    }
    return tmp;
}
var arr = [];
for(var i = 0; i < 3; i++) {
    arr[i] = prompt("Enter a number");
}
console.log(maxNum.apply(this, arr));

回答by dr jimbob

First in javascript and most modern programming language assignment like a = bcopies the value of binto the variable a. It is not equivalent to b = a(which copies the value of ainto the variable b). It's common to write a = 1, but a syntax error in most languages to write 1 = a. Thus, you don't want to write num1 = max, but instead write max = num1.

首先在 javascript 和大多数现代编程语言中赋值,例如a = b将 的值复制b到变量中a。它不等同于b = a(将 的值复制a到变量中b)。写是很常见的a = 1,但是在大多数语言中写是一个语法错误1 = a。因此,您不想写num1 = max,而是写max = num1

Second, your logic is incorrect as it won't treat the case maxNum(1,2,3)correctly. (Work through the logic when num1 < num2and num2 < num3. The following code would work:

其次,您的逻辑不正确,因为它不会maxNum(1,2,3)正确处理案例。(处理 whennum1 < num2和的逻辑num2 < num3。以下代码将起作用:

function maxNum(num1, num2, num3){
    var max = 0;
    if(num1 > num2){
        if(num1 > num3){
            max = num1;
        }
        else{
            max = num3;
        }
    }
    else{
        if(num2 > num3){
            max = num2;
        } else {
            max = num3;
        }
    }
    return max;
}

Granted, I would probably write something like

当然,我可能会写一些类似的东西

function max3num(num1, num2, num3) {
    var max_so_far = num1;
    if (num2 > max_so_far) {
       max_so_far = num2;
    }
    if (num3 > max_so_far) {
       max_so_far = num3;
    }
    return max_so_far;
}

as the logic is very clear and it will be easy to extend to a max function with a larger number of elements to compare if necessary. (Adding in a for loop could make it variadic fairly easily). It is straightforward to see the logic works, because we start with the first element being the maximum so far (max_so_far), then consider if the second element is larger -- if so we assign that as max_so_far, and keep continuing until we have compared all the elements to the max_so_far. After we have considered each element once, we then return the max_so_farwhich will now be the maximum of all of them.

因为逻辑非常清晰,如果需要的话,很容易扩展到具有更多元素进行比较的 max 函数。(添加 for 循环可以很容易地使其可变参数)。很容易看到逻辑是有效的,因为我们从第一个元素是迄今为止的最大值 ( max_so_far) 开始,然后考虑第二个元素是否更大——如果是,我们将其分配为max_so_far,并继续继续直到我们比较了所有元素元素到max_so_far. 在我们考虑了每个元素一次之后,我们然后返回max_so_far现在将是所有元素中最大值的 。

回答by Travis J

No real need for a function here, just compare them as the come in!

这里不需要真正的功能,只需在进来时比较它们!

var max = 0; 
for(var i=0;i<3;i++){ 
 var val = parseInt(prompt("Enter a number"));
 max = max > val ? max : val; 
} 
alert(max);

回答by Kimi2Greatness

The updated direct answer is this :

更新的直接答案是这样的:

function maxNum(num1, num2, num3){
    return [num1, num2, num3].sort(function (a, b) { return b - a })[0];
}

If written like this, it can easily be modified to take any amount of numbers by passing it an array of said numbers.

如果这样写,它可以很容易地修改为通过向它传递一个所述数字的数组来获取任意数量的数字。

var numArray = [num1, num2, num3, num4, ...];
function maxNum(numArray){
    return numArray.sort(function (a, b) { return b - a })[0];
}

The details :

细节 :

Take an array :

取一个数组:

[5,42,16]

Now sort it.

现在排序。

[5,42,16].sort()

But this wont work because javascript .sort requires a function to be passed in. This function tells it how to sort the array.

但这行不通,因为 javascript .sort 需要传入一个函数。这个函数告诉它如何对数组进行排序。

This will sort it highest to lowest, e.g. a is less then b.

这将把它从高到低排序,例如 a 小于 b。

function (a, b) { return b - a }

This will sort it lowest to highest, e.g. b is less then a.

这会将它从最低到最高排序,例如 b 小于 a。

function (a, b) { return a - b }

So we add it in :

所以我们添加它:

[5,42,16].sort(function (a, b) { return b - a })

But this returns the sorted array, not the maximum number.

但这返回排序后的数组,而不是最大数。

So we pick the first element in the array :

所以我们选择数组中的第一个元素:

[5,42,16].sort(function (a, b) { return b - a })[0]

Lastly, you can pull out the sort function. This is mostly for demo purposes though.

最后,您可以拉出排序功能。不过,这主要用于演示目的。

var maxSorter = function (a, b) { return b - a };
function maxNum(numArray){
    return numArray.sort(maxSorter)[0];
}