FAQ: Loops - The While Loop

I have a question on the random function. Usually, other languages ask for a random seed to assure random number sequence different on each run. I’m not sure if the Codecademy environment do that task or the Math library is the one that acomplish that.

JS on its own takes no seed for its Math.random() method, but the language permits us to override this with our own function. It’s complicated and will take some reading and research. This is where search comes in, and possibly SO.

CC does not control how JS behaves. That is all in the browser.

Thank you, It was glad to see you may get different random number sequences without additional effort on looking for a random seed, I just wanted to be sure it is JS, not on CC development environment.

1 Like

Different question.

How do I make the loop iterate until it meets the following condition:

The loop runs 20 times until it reached stop.
Meaning the loop runs once, reaches a stop in 1 iteration, keeps running because it didn’t meet the condition of 20 loops.

If we wish to write a conditional loop we have a few options.

while (condition) {
    // code

The above will run at least zero times. It can run indefinitely until the condition is not met.

do {
    // code
}  while (condition)

The above will run at least once. It can run indefinitely until the condition is not met.

for (let x of array) {
    if (condition) {

The above will iterate a definite number of times (the length of the array) unless a break condition exists.

1 Like

I got the same issue and still don’t understaand. I would be nice if someone could explain !!

We cannot explain what we cannot see. Please post the code you have so far. If it is copied from another post, chances are you copied their mistake, too.


That will make no sense since it is a string object, not a number.

Actually this exercise was a heavy first learning exercise (LOL) while it shows how a while loop eventually can be used, it raises fundamental questions about CONDITION matters…

To understand it I had to run it several times… because the loop script includes a random index to the card array variable (that leads to an unknown number of items with or without duplicates as a result diplayed always with ‘spade’ at the end if not only.

It was disturbing because the while loop condition is !=‘spade’. So I still had to figure out why the console logs it anyway at the end or only it. But it lead me to understand the meaning of the CONDITION related to the random INDEX attribution = AS LONG AS YOU WON’T GET INTO the card ‘spade’ index, don’t stop.

That doesn’t mean to exclude it from the result. It also shows that the loop works even starting from an undefined index and it’s good to know since the Math Random function will anyway set any number (in the exercise case from 0 to 3).

So in your trial (at least regarding the given exercise, you didn’t need to declare an iteration variable, neither i++ iteration after the math.random function to allow the loop to work because in this case, you only add 1 to an already random index number generated (I mean, in this particular case, the math random function is doing the job already).

1 Like

This answer was helpful in understanding what was really going on with the While loop in this exercise.
Thanks. I spent nearly 4 hours trying to understand it.

For those still confused I found the issue in my code was that I added currentCard++; which in this case wasn’t needed (as the randomiser changes the input each time) and was doing crazy things to the input - I guess trying to increment a word as a number?

Anyway, I reported the issue as it shouldn’t have passed as correct when it was clearly broken.

Hope this helps.

Im confused why this is printing ‘spade’ at all. If youre saying
“a condition that checks if the currentCard does not have that value `‘spade’”
spade has the value of spade.
in past loop examples with numbers they kinda say things like if the number is less than 4 it will run the loop, so 4 never prints because 4 is not less than 4. So why is spade printing when youre saying print what does not have the value of spade? Why wouldnt it stop and just not print the word spade? Does that question make sense?

It prints ‘spade’ because the logging takes place in the loop. The loop doesn’t terminate until ‘spade’ encountered, but we don’t know that until after the current iteration.

For this exercise, I have 2 questions that I must ask to completely understand everything:

  1. Shouldn’t the increment ‘counterTwo++’ reside BEFORE the ‘console.log(counterTwo)’ declaration, to be correct? I mean, to make a loop run, we have to use the incrementing statement within the parentheses, which gave me the opinion that the increment should always be first before printing the results, otherwise we would only get the printed number alone without the loop.

  2. In the exercise that we did, does the code within the block called “currentCard = cards[Math.floor(Math.random() * 4)];” act as the increment in this scenario, instead of the “++” thingies?

If anybody could please enlighten me about thee aspects, that would be fantastic!

In my opinion it is because of the place of console.log() in the block, it loggs the value of currentCard after the value is reassigned but before checking the stopping condition.
If you code it like this it wont log the ‘spade’:

let currentCard = ‘’;
while (currentCard != ‘spade’) {
currentCard = cards[Math.floor(Math.random() *4)];
Hope it helped :slight_smile:

How can you output the number of times this loop iterates?

There are different situations, and different approaches depending. Say your loop is building an array of some undetermined length. Would we need to count as we go? No. Just finish the loop and record the length of the array. That will be the number of iterations.

In a case where we don’t have anything to measure in the end, then initialize a counting variable before the loop.

count = 0

Now as the loop iterates, add one to the count each iteration, either before or after the loop body code. Once the loop ends, count will be the record of how many iterations.

cool ty. this is how i did it:
let currentCard;

let count = -1;

while(currentCard !== ‘spade’){

currentCard = cards[Math.floor(Math.random() * 4)];




console.log(You picked ${count} cards before a spade);

I’m not sure why I have to start count at -1 but that’s the only way it seemed to work.

That’s odd. It should start at zero if we are to obtain an accurate count.

What kind of error were you getting. The code looks fine assuming there is an array of cards.

It seems to be working now. If I set the count at 0, it includes the spade in the total. If I set count to -1, it only includes everything before spade. There must have been a glitch before cause I was getting an output of
stuff like that

1 Like

Recall that getting a spade still required an iteration of the loop. It is still technically a drawn card.