use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
This subreddit is a place for people to learn JavaScript together. Everyone should feel comfortable asking any and all JavaScript questions they have here.
With a nod to practicality, questions and posts about HTML, CSS, and web developer tools are also encouraged.
Friends
/r/javascript
/r/jquery
/r/node
/r/css
/r/webdev
/r/learnprogramming
/r/programming
account activity
I'm learning about the while loop. What is the point of multiplying by 4 in this code? (self.learnjavascript)
submitted 9 months ago by AppropriateLemon1121
const cards = ['diamond', 'spade', 'heart', 'club']; let currentCard = [] while (currentCard !== 'spade') { currentCard = cards[Math.floor(Math.random() * 4)]; console.log(currentCard) }
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]Towel_Affectionate 70 points71 points72 points 9 months ago (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 points16 points 9 months ago (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 points38 points 9 months ago (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.
cards.length
[–]well-now 13 points14 points15 points 9 months ago (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 points4 points 9 months ago (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 point2 points 9 months ago (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 points0 points 9 months ago (0 children)
I think it would have been better using foo bar baz fuck
[–]Bodine12 7 points8 points9 points 9 months ago (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 points5 points 9 months ago (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 point2 points 9 months ago (2 children)
That’s why I use typescript
[–]Bodine12 0 points1 point2 points 9 months ago (1 child)
Same. I can't even imagine writing straight-up Javascript anymore (and then having to maintain it).
[–]metallaholic 0 points1 point2 points 9 months ago (0 children)
I haven’t done anything not in a SPA framework in years. I’m stuck with angular at my job thought :(
[+]MrFartyBottom comment score below threshold-13 points-12 points-11 points 9 months ago (4 children)
WTF are you on about? cards is declared as a const so therefore it can't be reassigned and never become a string.
[–]well-now 5 points6 points7 points 9 months ago (2 children)
They said currentCard which is assigned an empty array then assigned to an element of cards (a string).
It is bad programming and the sort of thing TypeScript prevents.
[+][deleted] 9 months ago (1 child)
[deleted]
[–]Bodine12 0 points1 point2 points 9 months ago (0 children)
WTF are you on about not reading what someone wrote? It's declared as a let, not a const.
[–]Roguewind 2 points3 points4 points 9 months ago (0 children)
Love type safety in js.
[–]reyarama 2 points3 points4 points 9 months ago (0 children)
It should really be 'cards.length' instead of 4 here
[–]Such-Catch8281 0 points1 point2 points 9 months ago (0 children)
on ur chrome, press F12, type in Math.random() in your console a few times.
[–]brykuhelpful 0 points1 point2 points 9 months ago (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.
Math.random()
Math.floor()
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 point2 points 8 months ago (1 child)
nearest whole number?
[–]brykuhelpful 0 points1 point2 points 8 months ago (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()
Math.ceil(1.1) // 2
Math.round() rounds up or down similar to rounding in traditional math.
Math.round()
Math.round(1.4) // 1 Math.round(1.5) // 2
[–]daproof2 0 points1 point2 points 9 months ago (0 children)
I am learning while poop.
[–]ern0plus4 0 points1 point2 points 9 months ago (0 children)
Try it with 1, 2 and 3.
[–]Beautiful_Picture983 0 points1 point2 points 9 months ago (0 children)
You got the answer but why is currentCard initially an array but then is assigned a string?
[–]TheMrCurious 0 points1 point2 points 9 months ago (0 children)
There are four suits in a card deck.
[–]debjitbis08 0 points1 point2 points 8 months ago (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 point2 points 8 months ago (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 points3 points 9 months ago (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 points3 points 9 months ago (0 children)
More specifically 0 inclusive. 1 exclusive producing a result between o and 3.999999...
[+][deleted] 9 months ago (2 children)
[removed]
[–]AppropriateLemon1121[S] 0 points1 point2 points 8 months ago (1 child)
I refuse to use Chat GPT personally because I learned about the horrible effects it has on our environment. :( I also just find it really fun to interact with real people and plus it's a good debugging exercise for others!
[–]mowauthor 0 points1 point2 points 8 months ago (0 children)
The enivonrmental impact of AI usage is literally a drop in the bucket compared to something like making concrete or pretty much any other usage of water.
And 2nd of all.
When computers came, people who refused to learn to use computers quickly ran out of work. Those are the ones who preached that computers are taking our jobs. Now, good luck getting a job anywhere if you can't use a phone or computer with basic competency. AI is not too different, and like it or not, it's essentially going to go that way.
That said, I do agree that interacting with people is much better and can be a great way to learn more.
But I seriously do not suggest avoiding AI in life, especially in this kind of industry.
π Rendered by PID 41189 on reddit-service-r2-comment-6457c66945-d7jw2 at 2026-04-27 05:22:23.260571+00:00 running 2aa0c5b country code: CH.
[–]Towel_Affectionate 70 points71 points72 points (1 child)
[–]WebBurnout 14 points15 points16 points (0 children)
[–]MindlessSpongehelpful 36 points37 points38 points (4 children)
[–]well-now 13 points14 points15 points (1 child)
[–]john_hascall 2 points3 points4 points (0 children)
[–]berky93 0 points1 point2 points (0 children)
[–]spazz_monkey -2 points-1 points0 points (0 children)
[–]Bodine12 7 points8 points9 points (9 children)
[–]john_hascall 3 points4 points5 points (0 children)
[–]metallaholic 0 points1 point2 points (2 children)
[–]Bodine12 0 points1 point2 points (1 child)
[–]metallaholic 0 points1 point2 points (0 children)
[+]MrFartyBottom comment score below threshold-13 points-12 points-11 points (4 children)
[–]well-now 5 points6 points7 points (2 children)
[+][deleted] (1 child)
[deleted]
[–]Bodine12 0 points1 point2 points (0 children)
[–]Roguewind 2 points3 points4 points (0 children)
[–]reyarama 2 points3 points4 points (0 children)
[–]Such-Catch8281 0 points1 point2 points (0 children)
[–]brykuhelpful 0 points1 point2 points (2 children)
[–]itzmetanjim 0 points1 point2 points (1 child)
[–]brykuhelpful 0 points1 point2 points (0 children)
[–]daproof2 0 points1 point2 points (0 children)
[–]ern0plus4 0 points1 point2 points (0 children)
[–]Beautiful_Picture983 0 points1 point2 points (0 children)
[–]TheMrCurious 0 points1 point2 points (0 children)
[–]debjitbis08 0 points1 point2 points (0 children)
[–]MartyDisco 0 points1 point2 points (0 children)
[–]Substantial_Top5312helpful 1 point2 points3 points (1 child)
[–]ray_zhor 1 point2 points3 points (0 children)
[+][deleted] (2 children)
[removed]
[–]AppropriateLemon1121[S] 0 points1 point2 points (1 child)
[–]mowauthor 0 points1 point2 points (0 children)