FAQ: Loops - The While Loop


This community-built FAQ covers the “The While Loop” exercise from the lesson “Loops”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Web Development

Introduction To JavaScript

FAQs on the exercise The While Loop

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!


const cards = [‘diamond’, ‘spade’, ‘heart’, ‘club’];
let currentCard = ;
while (currentCard !== ‘spade’) {
currentCard = cards[Math.floor(Math.random() * 4)];

I don’t undestand what stop this code from running forever? The varible currentCard is not ‘spade’, soas I see it, it should run forever.



I had the same question but figured it out. A loop is gonna keep doing the same action over and over as long as it’s condition is true, meaning in this scenario that the random card is not ‘spade’. When it runs a few times eventually it’ll come across ‘spade’ which makes the condition false causing the loop to stop. This is why your output always ends with ‘spade’ but you won’t see it earlier.

Hope that helps. :wink:


How can you tell how many times it ran, before it found “spades”?


My buddy helped me with this:

const cards = [‘diamond’, ‘spade’, ‘heart’, ‘club’];

// Write your code below

let currentCard = "";

let i = 0;

while (currentCard != "spade"){

i = Math.floor(Math.random() * 4);

currentCard = cards[i];




//this outputs each random number and it’s pick


If the function produces random index [ 1 ], i.e. ‘spade’ the first time, it shouldn’t enter the while loop in the first place. Instead it prints ‘spade’ to the console. Why? Printing ‘spade’ means it entered the while loop and reached the ‘console.log’ line.


Sorry - I figured it out: the first time around currentCard is not yet ‘spade’, it’s undefined. That’s how it enters the loop!


Hey guys,

I must’ve done something wrong in this because the console is miles long and spade appears many times before it stops running (slowing down chrome significantly).

What have I done wrong?

Here’s my code:

const cards = [‘diamond’, ‘spade’, ‘heart’, ‘club’];

// Write your code below

let currentCard;

while (currentCard !== ‘spade’) {
currentCard = cards[Math.floor(Math.random() * 4)];


That is a very good question. I added an extra console.log to your code. Try this and i think you can find why it keeps on looping even after finding spade.

const cards = ['diamond', 'spade', 'heart', 'club'];

// Write your code below

let currentCard;

while (currentCard !== 'spade') {
currentCard = cards[Math.floor(Math.random() * 4)];


Thanks for the quick answer biirra!
I see my error now :slight_smile:

Edit: I asked a stupid question and found my answer from googling :slight_smile: Thanks!


This threw me for a loop :sweat_smile: - jk. But seriously, how was it decided to use ’ 4’, instead of any other number?* Is there a specific reason? I’ve tested the loop with *10, *3, and so on. It seems to work, but also prints ‘undefined’ at times. Running it with *1 will not print to the console.


Ha :slight_smile: When generating the index of the current card, we multiply by 4 here because there are 4 elements in the cards array:

Math.floor(Math.random() * 4)

This will give us numbers from 0 to 3 (explained below). That’s what we need, because cards has 4 elements and array indexes start at 0, so the card indexes run from 0 to 3.

Math.random generates numbers from 0 to something like 0.9999999999999999, while Math.floor rounds numbers down to the nearest integer. So from our code, zero is the minimum number we can get:

Math.floor(Math.random() * 4)
Math.floor(0 * 4)

And three is the maximum:

Math.floor(Math.random() * 4)
Math.floor(0.999... * 4)

If you multiply by a number greater than 4, it will sometimes print undefined because you will generate nonexistent card indexes (like 4, 5, 6, etc.).

If you multiply by 1, on the other hand, you will create an infinite loop. That’s because you will always generate a card index of 0, but the spade card has an index of 1. You’re looping until you find the spade, but that will never happen, so your loop will run forever. I’m guessing nothing printed to the console for you because of some error related to that.


That makes total sense now. And yes, I had the refresh the page several times when running it with ‘*1’ :sweat_smile:.

Basically, in order to generate this task, we must know the length of the array first, right? The reason I tired various options was (1) I was trying to learn what happens, since I wasn’t clear on why 4 was used, and (2) if I didn’t know the specific length of an array. But, I guess that’s a quick line of code to identify the length before executing the task.

Greatly appreciated!


I had the refresh the page several times when running it with ‘*1’ :sweat_smile:

I was fooling around in JSFiddle and managed to hang up my whole browser while testing that part out. :slight_smile:

Yep, we do need to know the array length. But like you indicate, we can just check the length property of the array.

Glad I could help!


Could you please explain what happens? when I pasted this code, the page froze.


It’s not that its fronzen it’s because this piece of code does not do anything visual on the page.

If you press “F12” and you click the “console” tab in the window that pops up you will see the result.
This because console.log(); prints to that tab.

This is also where errors in your code are shown.