Help with while loops

Hi all, currently going over while loops here:

I am getting what i believe is an infinite loop which is causing the lesson to not tick off.

My code looks like this and looks ok to me, am I doing something wrong?

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

// Write your code below

let currentCard 

while (currentCard != 'spade') {

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

  console.log (currentCard)



So the loop should stop when the the card does not = ‘spade’?
It goes on forever printing random cards.

Just need some clarity if possible.

The loop runs as long as the condition is true, so:

'diamond' != 'spade'

is true. So the loop will make another iterations. The only way for this loop to become false:

'spade' != 'spade'

this is false, so the loop stops. So you keep drafting cards until you draw a spade

The problem is doing currentCard++, why would you do this? Given currentCard is a string, trying to increase its value by one, currentCard gets a value of NaN (not a number)

1 Like

Hmm that makes sense. I used it because I thought it to be an integral part of loops, I’ve seen ++ used in every example so far.

This is even written in the same lesson:

What would happen if we didn’t increment counterTwo inside our block? If we didn’t include this, counterTwo would always have its initial value, 1 . That would mean the testing condition counterTwo < 4 would always evaluate to true and our loop would never stop running! This is called an infinite loop and it’s something we always want to avoid . Infinite loops can take up all of your computer’s processing power potentially freezing your computer.

Obviously it’s talking about a different example but you can see where I am coming from.

Thanks for the help.

Quite common, but then the loop condition often involves numbers. Which is not the case here.

we change the initial value, just not using numbers.

These kind of exercises are very good, because they force you to think about the why question. Why should I should I use numbers? Or not? Or even more in general: Why would I need to use concept A or B and what is the best fit here? These thinking steps are so crucial to learn :slight_smile:

1 Like

Very true. I appreciate you taking the time to reply.

I actually decided to go back and redo these sections as I was having a really difficult time with the JS challenges in future steps. Thinking about it now this is probably one of the reasons why.

To provide an example of what @stetim94 said:
If I want to print out 5 numbers-1,2,3,4,5; I could could use the incremental operator:

let val_a = 1;
while (val_a < 6) {
val_a ++;

This will print out:


If you want to iterate over non-integer/float values, the ++ operator doesn’t do much:

let str_val = "a";
while (str_val != "abbb") {
str_val += "b";//in this case the += is the concatenation operator-concatenating the string
//on the right with the string on the left

This will print:


In this circumstance, we used another method of stopping an infinite loop-string concatenation.

I hope this was helpful!

1 Like

Very helpful thank you.

On a slightly unrelated note. One of the main things I am currently struggling with is creating functions that loop through arrays.

One of the challenges was to create a function that loops through an array of strings and concatenate a ‘!’ on the end.

How I would approach this would be using a for loop :

const myFunction = (arr) => {
for (i = 0; i < arr.length; i++ ) {
arr [i] += ‘!’

Is this working in a similar fashion to my previous mistake?
As in, I’m trying to increment a string? Therefore its NaN?

1 Like

you are not incrementing the string, increment is a word that applies to numbers. What you do here:

arr [i] += '!'

is known as string concatenation.

seems to work fine? except when you pass an array directly to the function, you have no way to access it later.

1 Like