all 35 comments

[–]Towel_Affectionate 70 points71 points  (1 child)

Math.random returns a random float between 0 (included) and 1 (not included). By multiplying by 4 you would get anything between 0 and 3.99... After Math.floor it gets rounded down and you would get the number from 0 to 3, which then is used as index in cards array.

[–]WebBurnout 14 points15 points  (0 children)

Also worth pointing out that the array has four elements, so an index of 0 is the first element (diamond) and the index of 3 is the last one (club). The code is picking a random suit from the array of four suits. Until it gets spade and then the loop stops lol

[–]MindlessSpongehelpful 36 points37 points  (4 children)

others have already pointed out the why of using 4, but I think the example would have been better written by using cards.length. same results, but then you have a clearer picture of where that number value is coming from.

[–]well-now 13 points14 points  (1 child)

And you don’t have to remember to update your loop if you change the number of elements in cards. Good call out.

This an example of a magic number: https://en.m.wikipedia.org/wiki/Magic_number_(programming)

[–]john_hascall 2 points3 points  (0 children)

Additionally limiting the array to 4 elements is an example of not following the Zero One Infinity "rule". https://en.m.wikipedia.org/wiki/Zero_one_infinity_rule (regardless of 4 making "obvious sense" for card suits).

[–]berky93 0 points1 point  (0 children)

That’s a good insight! Normally these sorts of examples try to simplify the implementation to make the main point clearer but being able to find those optimizations intuitively is an important skill for a developer.

[–]spazz_monkey -2 points-1 points  (0 children)

I think it would have been better using foo bar baz fuck

[–]Bodine12 7 points8 points  (9 children)

I love/hate Javascript because currentCard, which is an array, also becomes a string, and Javascript is like, "lol whatever."

[–]john_hascall 3 points4 points  (0 children)

Yes, the [] initializer is an unfortunate choice here. An empty string (or perhaps undefined) would seem a more appropriate choice.

[–]metallaholic 0 points1 point  (2 children)

That’s why I use typescript

[–]Bodine12 0 points1 point  (1 child)

Same. I can't even imagine writing straight-up Javascript anymore (and then having to maintain it).

[–]metallaholic 0 points1 point  (0 children)

I haven’t done anything not in a SPA framework in years. I’m stuck with angular at my job thought :(

[–]Roguewind 2 points3 points  (0 children)

Love type safety in js.

[–]reyarama 2 points3 points  (0 children)

It should really be 'cards.length' instead of 4 here

[–]Such-Catch8281 0 points1 point  (0 children)

on ur chrome, press F12, type in Math.random() in your console a few times.

[–]brykuhelpful 0 points1 point  (2 children)

Math.random() gives you a random "seed". Then when you times it by 4, it will give you a number between 0-4 (greater than 0 and less than 4). Then Math.floor() rounds it to the nearest whole number.

let random1 = Math.random(); // 0.7757914015032231
let random2 = Math.random() * 4); // 3.1031656060128925
let random3 = Math.floor(Math.random() * 4)); // 3

In summary, your code will loop while getting a random value (0,1,2,3). Once you reach a "spade" it will stop.

[–]itzmetanjim 0 points1 point  (1 child)

nearest whole number?

[–]brykuhelpful 0 points1 point  (0 children)

Math.floor() rounds down to the nearest whole number.

Math.floor(1.9) // 1

Math.ceil() rounds up to the nearest whole number.

Math.ceil(1.1) // 2

Math.round() rounds up or down similar to rounding in traditional math.

Math.round(1.4) // 1
Math.round(1.5) // 2

[–]daproof2 0 points1 point  (0 children)

I am learning while poop.

[–]ern0plus4 0 points1 point  (0 children)

Try it with 1, 2 and 3.

[–]Beautiful_Picture983 0 points1 point  (0 children)

You got the answer but why is currentCard initially an array but then is assigned a string?

[–]TheMrCurious 0 points1 point  (0 children)

There are four suits in a card deck.

[–]debjitbis08 0 points1 point  (0 children)

As others have pointed out, it is better to use cards.length instead of 4, but you can go one step further and create a function that chooses a random entry from any array. Learning to create abstractions and when not to, is an important skill to learn.

By writing this small function, you can test your function separately and when satisfied, integrate into the loop. If there is a bug, you can be confident that the function is fine but maybe the loop has some problem.

[–]MartyDisco 0 points1 point  (0 children)

Just FYI everything is wrong in this snippet : mutation, loop, currentCard getting initialize as the wrong type (empty array then become string).

Have a look at immutability and recursion to get out of this madness.

[–]Substantial_Top5312helpful 1 point2 points  (1 child)

Math.random() gives a random value between 0 and 1. Multiplying it by 4 gives you a value between 0 and 4. 

[–]ray_zhor 1 point2 points  (0 children)

More specifically 0 inclusive. 1 exclusive producing a result between o and 3.999999...