Javascript I';我试图创建一个函数,将范围(0-100)内的三个数字随机化,并打印最大的一个

Javascript I';我试图创建一个函数,将范围(0-100)内的三个数字随机化,并打印最大的一个,javascript,math,random,Javascript,Math,Random,。。。但是当我在控制台中调用函数时,它返回未定义的。我是一个JavaScript新手,所以我可能犯了一个基本的错误,如果有人能帮助我,我将不胜感激:-) 代码如下: var randomPrint = function(){ x = Math.floor(Math.random() * 100); y = Math.floor(Math.random() * 100); z = Math.floor(Math.random() * 100); console.log(x, y, z);

。。。但是当我在控制台中调用函数时,它返回未定义的。我是一个JavaScript新手,所以我可能犯了一个基本的错误,如果有人能帮助我,我将不胜感激:-)

代码如下:

var randomPrint = function(){

x = Math.floor(Math.random() * 100);
y = Math.floor(Math.random() * 100);
z = Math.floor(Math.random() * 100);

   console.log(x, y, z);

   if(x > y && x > z)
   {
     console.log("The greatest number is" + " " + x);
   }
   else if(y > z && y > x)
   { 
     console.log("The greatest number is" + " " + y);
   }
   else if(z > y && z > x)
   {   
    console.log("The greatest number is" + " " + z);
   }
};
randomPrint();
理智的方法:

var nums = [];
for (var i = 0; i < 3; i++) {
    nums.push(Math.floor(Math.random() * 100));
}

console.log('Largest number is ' + Math.max.apply(null, nums));
是的,函数将返回
未定义的
,因为您没有从函数返回任何内容。很可能您的条件都不匹配,因此您也看不到任何其他输出。

明智的做法是:

var nums = [];
for (var i = 0; i < 3; i++) {
    nums.push(Math.floor(Math.random() * 100));
}

console.log('Largest number is ' + Math.max.apply(null, nums));

是的,函数将返回
未定义的
,因为您没有从函数返回任何内容。很可能您的条件都不匹配,因此您也看不到任何其他输出。

尝试此内置方法以获得最大值

Math.max(x,y,z);

试试这个内置的方法来获得最大值

Math.max(x,y,z);

deceze给出的答案是一个更好的解决方案,但我认为您也在工作。控制台中的输出示例如下:

35 50 47
The greatest number is 50
undefined
未定义的部分是因为函数没有返回任何内容。你可以把它写成

var randomPrint = function(){

    x = Math.floor(Math.random() * 100);
    y = Math.floor(Math.random() * 100);
    z = Math.floor(Math.random() * 100);

   console.log(x, y, z);

   if(x > y && x > z) {
        var biggest = x;
        console.log("The greatest number is" + " " + x);
   } else if(y > z && y > x) { 
       console.log("The greatest number is" + " " + y);
       var biggest = y;
   } else if(z > y && z > x) {   
       console.log("The greatest number is" + " " + z);
       var biggest = z;
   }
   return biggest;
};

randomPrint();

deceze给出的答案是一个更好的解决方案,但我认为您也在工作。控制台中的输出示例如下:

35 50 47
The greatest number is 50
undefined
未定义的部分是因为函数没有返回任何内容。你可以把它写成

var randomPrint = function(){

    x = Math.floor(Math.random() * 100);
    y = Math.floor(Math.random() * 100);
    z = Math.floor(Math.random() * 100);

   console.log(x, y, z);

   if(x > y && x > z) {
        var biggest = x;
        console.log("The greatest number is" + " " + x);
   } else if(y > z && y > x) { 
       console.log("The greatest number is" + " " + y);
       var biggest = y;
   } else if(z > y && z > x) {   
       console.log("The greatest number is" + " " + z);
       var biggest = z;
   }
   return biggest;
};

randomPrint();
你的逻辑也没有错。“未定义”可能会出现在其他地方,这很好

88 36 15 localhost/:16 最大的数字是88

这是我得到的你的代码的输出

你的逻辑也没有错。“未定义”可能会出现在其他地方,这很好

88 36 15 localhost/:16 最大的数字是88


这是我得到的代码的输出。

如果您可以扔掉其他两个数字:

for (var i = 0, max = -Infinity; i < 3; ++i) {
    max = Math.max(Math.floor(Math.random() * 100), max);
}

alert(max);
for(变量i=0,max=-无穷大;i<3;++i){
max=Math.max(Math.floor(Math.random()*100),max);
}
警报(最大值);

如果您可以扔掉其他两个数字:

for (var i = 0, max = -Infinity; i < 3; ++i) {
    max = Math.max(Math.floor(Math.random() * 100), max);
}

alert(max);
for(变量i=0,max=-无穷大;i<3;++i){
max=Math.max(Math.floor(Math.random()*100),max);
}
警报(最大值);

问题是,如果两个数字相等,则此操作失败。是的,我也注意到了这一点,但没有提及,因为这不是问题的一部分。让他在这个问题上再想一想,他正在学习js:)问题是,如果两个数字相等,这就失败了。是的,我也注意到了,但没有提到,因为这不是问题的一部分。让他在这个问题上再想一想,他正在学习js:)我想指出的是,如果两个最大的数字相等,你就不会打印任何东西。至于问题:未定义的数字在哪里打印?我想指出,如果两个最大的数字相等,你将不会打印任何东西。至于问题:未定义的文件打印在哪里?