Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/462.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 什么';检测变量中最高值的最短方法是什么?_Javascript - Fatal编程技术网

Javascript 什么';检测变量中最高值的最短方法是什么?

Javascript 什么';检测变量中最高值的最短方法是什么?,javascript,Javascript,我有三个不同输出的变量: a = parseInt(Math.random() * 10000); b = parseInt(Math.random() * 10000); c = parseInt(Math.random() * 10000); 假设任何变量都不相等,我如何检测哪个值最高?只需使用Math.max()方法: var max=Math.max(a、b、c)使用 a=parseInt(Math.random()*10000); b=parseInt(Math.random()*

我有三个不同输出的变量:

a = parseInt(Math.random() * 10000);
b = parseInt(Math.random() * 10000);
c = parseInt(Math.random() * 10000);

假设任何变量都不相等,我如何检测哪个值最高?

只需使用
Math.max()
方法:

var max=Math.max(a、b、c)

使用

a=parseInt(Math.random()*10000);
b=parseInt(Math.random()*10000);
c=parseInt(Math.random()*10000);
var max=数学最大值(a、b、c);

控制台日志(a、b、c、最大值)当然,如果输入的数量可变,只需使用
Array.prototype.reduce()

这是@thaadikkaran在自删除评论(AFAIR)中发布的内容

这里有一种方法,它将生成然后给出最大值,尽管在实践中使用
Match.random()
这样做没有意义:

Array(3).fill(Math.random() * 10000).reduce((a,b) => Math.max(a,b))
简洁性的赢家(如果您遵循以下规则):

然后可以生成性地使用:

Math.max(...Array(3).fill(Math.random() * 10000))
e、 g:

如果使用下划线

let a = parseInt(Math.random() * 10000);
let b = parseInt(Math.random() * 10000);
let c = parseInt(Math.random() * 10000);
let max = _.max([a,b,c])
console.log(max)
你可以用一堆

我建议使用,因为它是由数字和它们的整数等效而成的。该函数用于对具有给定基数的数字进行字符串解析

var a=Math.floor(Math.random()*10000),
b=数学地板(数学随机()*10000),
c=数学地板(数学随机()*10000),
最大值=a>b
? a>c
? A.
:c
:b>c
? B
:c;
控制台日志(a、b、c、最大值)

.as console wrapper{max height:100%!important;top:0;}
您应该将这些变量的名称放入一个数组,并创建另一个数组来包含每个变量的相关值,我的脚本如下:

vararray1=['a','b','c'];
var array2=[];
函数findMaxVar(){

例如(让i=0;答案总是C。lolI觉得有一个我不知道的命令可以很容易地解决这个问题。有
Math.max(a,b,C)
如果只有三个,但我不知道它是否匹配“最短路径”。有不同的解释方法(最少的击键,最有效的算法,等等)@thaadikkaran如果有很多值是可变的,那么这就是一个很好的建议。谢谢你,这正是我想要的!酷,如果你愿意,你可以接受并向上投票我的答案=)ofc,而不是将变量设置为
a
b
,或者
c
OP必须使用
(array).push(value)
。@TyQ.这是一种方法,当然,如果它是像这样的类型的东西,那么它也可以是:
array(3).fill(Math.random()*10000).reduce((a,b)=>Math.max(a,b))
更简单地使用
Math.max.apply
在es6中的数组或扩展运算符上…
Math.max(…arr)
@charlietfl是的,我正试图弄清楚如何使用扩展运算符。这至少让人更容易理解。
Math.max(...Array(3).fill(Math.random() * 10000))
function maxFromRandom(n, s) {
    return Math.max(
        ...Array(n).fill(Math.random() * s)
    )
} 
maxFromRandom(3, 10000)
let a = parseInt(Math.random() * 10000);
let b = parseInt(Math.random() * 10000);
let c = parseInt(Math.random() * 10000);
let max = _.max([a,b,c])
console.log(max)