explanation #134155
-
Select Topic AreaShow & Tell BodyCan somebody step by step explain me how my function work? function changeColor(){ for (var i=0; i < 6; i++){ |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
|
Math.random() generates a random decimal number between 0 (inclusive) and 1 (exclusive). This loop will iterate 6 times, appending a random number from hex_numbers randomly each time. At the end of the loop hexcode will be a string with 6 numbers corresponding to the 6 characters in a hexadecimal color code (e.g. #RRGGBB). |
Beta Was this translation helpful? Give feedback.
Math.random() generates a random decimal number between 0 (inclusive) and 1 (exclusive).
Multiplying by hex_numbers.length (which is 15) scales this random number to the range of the array indices.
Math.floor() rounds down to the nearest integer, giving a valid index for the array.
This loop will iterate 6 times, appending a random number from hex_numbers randomly each time. At the end of the loop hexcode will be a string with 6 numbers corresponding to the 6 characters in a hexadecimal color code (e.g. #RRGGBB).