# 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:

## 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 () 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.

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

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 = ;
currentCard = cards[Math.floor(Math.random() * 4)];
console.log(currentCard);
}

Hi,
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.
Thank’s.

4 Likes

Hi!

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.

11 Likes

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’];

let currentCard = "";

let i = 0;

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

currentCard = cards[i];

console.log(currentCard);

console.log(i);

}

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

1 Like

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!

1 Like

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’];

let currentCard;

currentCard = cards[Math.floor(Math.random() * 4)];
console.log(currentCard);
currentCard++;
}

1 Like

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'];

let currentCard;

currentCard = cards[Math.floor(Math.random() * 4)];
console.log(currentCard);
currentCard++;
console.log(currentCard);
}
``````
2 Likes

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

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

1 Like

This threw me for a loop - 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.

2 Likes

Ha 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)
Math.floor(0)
0
``````

And three is the maximum:

``````Math.floor(Math.random() * 4)
Math.floor(0.999... * 4)
Math.floor(3.999...)
3
``````

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.

5 Likes

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

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!

2 Likes

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

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

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

2 Likes

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.

Hi,

could be:

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

let currentCard;
let times = 0;
currentCard = cards[Math.floor(Math.random() * 4)];
times++
console.log(currentCard);
console.log(times);
}
//this output the card picked and the round

Hello,
I just learned how to do the “for” looping. And the rule was you had to have 3 things between semi-colons.
Now the “while” looping doesn’t require that. So, I’m confused. thank you!

I ran it “successfully” so I can go to next lesson; but I’m unsatisfied. Logging the code produced 6 cards. I think currentCard should only show 1 card?

I’ll give explaining the difference a shot.

The for loop has 3 parts in it's initializing statement:
for (initialize the index variable; index variable stop condition; increment index variable)
``````for ( let i = 0; i < 3; i++) {
//The code in this block will be executed 3 times
//Once for each value of i: 0, 1, 2
console.log(i);
}
``````

Output:

0
1
2

The while loop only has a stop condition:

``````let count = 0;
while (count <= 10) {
/*The code in this block will repeat until count is greater than 10*/
console.log(count);
count++;
}
``````

Output:

0
1
2
3
4
5
6
7
8
9
10

Hopefully this helps.

1 Like