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) {
        break
    }
}

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

1 Like

Hi
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.

currentCard++

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’) {
console.log(currentCard);
currentCard = cards[Math.floor(Math.random() *4)];
}
Hope it helped :slight_smile: