admin管理员组文章数量:1421951
backgroundColor=['red','green','blue','orange'][Math.floor(Math.random()*4)]
Can someone explain how this code executes in the Javascript syntax, I understand it generates a random number but the result is the color that is in the index value generated in the second square bracket.
In C++ array[][] is used to generate a two-dimensional array but here it is generating the index to be used in the first square bracket pair.
Hope to get clarification on how code execution happens
backgroundColor=['red','green','blue','orange'][Math.floor(Math.random()*4)]
Can someone explain how this code executes in the Javascript syntax, I understand it generates a random number but the result is the color that is in the index value generated in the second square bracket.
In C++ array[][] is used to generate a two-dimensional array but here it is generating the index to be used in the first square bracket pair.
Hope to get clarification on how code execution happens
Share Improve this question edited Oct 8, 2020 at 8:43 Phil 165k25 gold badges262 silver badges267 bronze badges asked Oct 8, 2020 at 8:37 Harsh BhudoliaHarsh Bhudolia 1539 bronze badges 1-
2
['red','green','blue','orange']
defines an array,['red','green','blue','orange'][x]
gets the item with the indexx
from the array. Thex
can be an expression, so['red','green','blue','orange'][Math.floor(Math.random()*4)]
gets the item with an random index from 0 to 3 from the array. – Hao Wu Commented Oct 8, 2020 at 8:40
4 Answers
Reset to default 7It's easier if you break this down into individual parts
const colors = ['red','green','blue','orange']
const randomIndex = Math.floor(Math.random()*4)
// colors.length would be even better than "4"
// Pick a random index from "colors"
backgroundColor = colors[randomIndex]
// or, expanding "randomIndex"
backgroundColor = colors[Math.floor(Math.random()*4)]
// or, expanding "colors"
backgroundColor = ['red','green','blue','orange'][Math.floor(Math.random()*4)]
What you are doing with Math.floor(Math.random()*4) is providing index up to 4 elements for the array.
I suggest you use Quokka plugin for Visual Studio Code and see how things work.
See the picture below from Quokka.
Basically you have three parts:
backgroundColor = ['red', 'green', 'blue', 'orange'][Math.floor(Math.random() * 4)];
^^^^^^^^^^^^^^^^^
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
- LHS assignment
- an array expression
- an property accessor in bracket notation with another expression.
The problem arises if you separate the property accessor from the line and move it to the next line or use another array expression without separating the expression to prevent another array as property accessor, like
x = [42, 15]
[1, 2, 3].forEach(x => console.log(x))
More to read: What are the rules for JavaScript's automatic semicolon insertion (ASI)?
The first bracket is the array.
The second bracket is the index you want to access.
Math.floor(Math.random()*4)
resolves to an number somewhere between 0 and 4
console.log(Math.floor(Math.random()*4))
Multidimensional arrays can be done with nested arrays like:
let arr = [[1,2,3], [3,4,5], [5,6,7]]
A funny thing is that you can access object properties with the same syntax
let obj = {
1: "hy"
}
console.log(obj[1])
At the first place if you see obj[1]
you would think its an array, but its actually an object. Need to watch out for things like this
本文标签:
版权声明:本文标题:arrays - How does the double square bracket notation in Javascript works when one of them is used to generate a random number? - 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745360484a2655270.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论