简体   繁体   English

从数组检索的var是不确定的?

[英]The var retrieved from an array is undefined?

The following code includes an array called buttonColours, a random number of 0 to 3, then the randomColour is supposed to be a random colour in the array of buttonColours, but it doesn't. 下面的代码包括一个名为buttonColours的数组,其随机数为0到3,然后将randomColour假定为buttonColours数组中的随机颜色,但事实并非如此。 It classifies randomColour as undefined. 它将randomColour分类为未定义。

 var buttonColours = ["blue", "red", "green"]; var randomNumber = Math.floor(Math.random * 3); var randomColour = buttonColours[randomNumber]; console.log(randomColour); 

The console.log only logs randomColor as undefined. console.log仅将randomColor记录为未定义。 Is there a reason why? 有什么理由吗?

You'll know directly the problem if you did a console.log on each line : 如果您在每一行上执行console.log,您将直接知道问题所在:

 var buttonColours = ["blue", "red", "green"]; console.log(buttonColours); // ["blue", "red", "green"] var randomNumber = Math.floor(Math.random * 3); console.log(randomNumber); // NaN var randomColour = buttonColours[randomNumber]; console.log(randomColour); // undefined 

The problem is your Math.floor(Math.random * 3) is returning NaN, as it should be Math.random() 问题是您的Math.floor(Math.random * 3)返回NaN,因为它应该是Math.random()

 var buttonColours = ["blue", "red", "green"]; var randomNumber = Math.floor(Math.random() * 3); var randomColour = buttonColours[randomNumber]; console.log(randomColour); 

I think this is what you want to achieve. 我认为这是您想要实现的。

If you will try to find out the result of each line you will come to know that Math.floor(Math.random * 3) is returning 'Nan' so the finally when you try to select the element of that position will be undefined . 如果您尝试找出每一行的结果,您将知道Math.floor(Math.random * 3)返回'Nan'因此最终当您尝试选择该位置的元素时将是undefined

This is returning Nan Because Math.random is a function and you need () to call a function. 这是返回Nan因为Math.random是一个函数,您需要()才能调用一个函数。 As you can see in my code it is Math.random() 您可以在我的代码中看到它是Math.random()

 var buttonColours = ["blue", "red", "green"]; var randomColour = buttonColours[Math.floor(Math.random() * buttonColours.length)]; console.log(randomColour); 

You can get a random number within a range by this function. 您可以通过此功能获得一定范围内的随机数。 The Math.random() return a random value within [0,1). Math.random()返回[0,1)内的随机值。 So you have to map the [0,1) with the range you given. 因此,您必须将[0,1)与给定的范围进行映射。

function getRandom(min, max) {
  return Math.floor(Math.random() * (max-min) + min);
}

See your solution in this snippet- 在此代码段中查看您的解决方案-

 var buttonColours = ["blue", "red", "green"]; var randomNumber = getRandom(0, buttonColours.length); var randomColour = buttonColours[randomNumber]; console.log(randomColour); function getRandom(min, max) { return Math.floor(Math.random() * (max-min) + min); } 

The problem is here: 问题在这里:

var randomNumber = Math.floor(Math.random * 3);

Math.random() is a method, not a property, of the Math object, hence it needs to be called . Math.random()是Math对象的方法,而不是属性,因此需要调用 Calling is done by following the name of the function with a pair of parentheses, so to call a function named sayHello, I would do: 调用是通过在函数名称后加上一对括号来完成的,因此要调用名为sayHello的函数,我会这样做:

sayHello()

So to fix your problem, place a pair of parentheses after Math.random, so that line looks like so: 因此,要解决您的问题,请在Math.random之后放置一对括号,使该行如下所示:

var randomNumber = Math.floo(Math.random() * 3);

If you want to know what Math.random() actually does, look at MDN's documentation for it : 如果您想知道Math.random()的实际作用,请查看MDN的文档

The Math.random() function returns a floating-point, pseudo-random number in the range 0–1 (inclusive of 0, but not 1)... Math.random()函数返回一个浮点伪随机数,范围为0-1(包括0,但不包括1)...

So essentially it returns a random number from 0 to 0.9999999..., which is why your line of code: 因此,从本质上讲,它返回一个从0到0.9999999 ...的随机数,这就是您的代码行的原因:

Math.floor(Math.random() * 3)

Returns an integer 0, 1 or 2, but not 3. 返回整数0、1或2,但不返回3。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM