JavaScript code for this example?

Hello to all! I completed several short courses on JavaScript and now I moved on to Heads Up: JavaScript which was very fun and helps to solidify my learning, However, I ran into something that I didn't understand. In the following code snippet, I understand what the program usually executes when it is executed, but trying to trace each step of execution, I realized that I was confused about "What / Why / How" for a particular segment. Here's the code for a sample program I'm looking at:


function makePhrases() {

            var words1 = ["24/7", "multi-tier", "30,000 foot", "B-to-B", "win-win"];

            var words2 = ["empowered", "value-added", "oriented", "focused", "aligned"];

            var words3 = ["process", "solution", "tipping-point", "strategy", "vision"];



            var rand1 = Math.floor(Math.random() * words1.length);

            var rand2 = Math.floor(Math.random() * words2.length);

            var rand3 = Math.floor(Math.random() * words3.length);



            var phrase = words1[rand1] + " " + words2[rand2] + " " + words3[rand3];

            alert(phrase);

        }

        makePhrases();

      


This is the segment that is confusing me:

            var rand1 = Math.floor(Math.random() * words1.length);

            var rand2 = Math.floor(Math.random() * words2.length);

            var rand3 = Math.floor(Math.random() * words3.length);

      

I get that this is a piece of code that randomizes which element from each array is fetched to form a new "random phrase", but I don't understand how this is done. I also didn't know before that Math.random or Math.floor can be applied to strings (must it be because they are in an array that is essentially a number?), Or how / why to use Math.random or Math.floor with strings.

Also, why do we need to use .length with this incarnation? What does it do? I appreciate your wisdom here and take the time to help someone new to coding and still have so much to learn!

+3


source to share


4 answers


Look at the code:

var rand1 = Math.floor(Math.random() * words1.length);

      

Math.random()

returns a number between 0

and 0.999999..

.

words1

- a list of words to choose from.

words1.length

is the size of the list, the number of elements, 5

in this case.



Math.random() * words1.length

returns a number between 0

and 4.99999..

.

Finally, use Math.floor()

to get an integer between 0

and 4

.

This number is then used as an index in words1

, therefore words1[rand1]

.


So, operations are Math

never used in a row, fetching a row only in the last step.

+3


source


All that happens is Math.random () is used as a multiplier against the number of elements in the corresponding arrays (property ".length") to create the index value. It does not apply to a string; as part of an expression to determine an index into a string array.



+2


source


You want to select a random item from an array. So you need an index, in other words, a random number from 0

to 4

(because your length is 5

). Math.random

will give you a random number between 0

and 1

(excluding 1). Therefore, in order to turn this into a random number between 0 and 4, you need several in length 5

.

Then, since we want an integer, not a floating point number, we use it Math.floor

to truncate it to an integer.

0


source


Math.random()  //Return a random number between 0-1
words1.length()  //Return the length of the array
Math.floor()     //Return the closest integer less than or equal to a given number.

      

Now the expressions:

(Math.random() * words1.length) 

      

Returns a random number between 0 and the length of the array. There could be a float like 3,4:

Math.floor(Math.random() * words1.length)

      

Will return a number an integer between 0 and the length of the string, so you can use it as an indexer (behaving like an array).

Note: Note that the random number is between 0 (inclusive) and 1 (exclusive), so it is safe to use Math.floor()

to avoid an exception and why it is not used Math.ceiling

.

0


source







All Articles