6/8 While Loops: Why commenting console.log(currentCard); returns only "Spade" as output?


#1
Hello,

I was just wondering what this “console.log(currentCard);” is actually doing in the code.
Because if I put comment this one line like this “//console.log(currentCard);”… the output is just “Spade”.

Can somebody, please guide me, what is happening in the background?

BR,
<Below this line, add a link to the EXACT exercise that you are stuck at.>
https://www.codecademy.com/courses/learn-javascript-loops/lessons/loops/exercises/while?action=lesson_resume&course_redirect=introduction-to-javascript

<Below this line, in what way does your code behave incorrectly? Include ALL error messages.>

```

//console.log(currentCard);


#2

A console.log() statement in a loop is very useful @ajaxpro05326 with every “iteration” of the loop, it gives an output to the console…

In the following example a while loop contains the value and changes of n, each iteration increases the value by 1, however we dont get to see this everytime the loop executes without the help of console.log(), but we will see the final value/output which is assigned to x in the example :

var n = 0;
var x = 0;
while (n < 3) {
  n++;
console.log(n);
//what is the value of n during this iteration?
  x += n;
}

#3

I think value of n is zero during this iteration. And it is because (as far as I understood) console.log is used to print something on screen and does not change the value of variables.


#4

Within the code block, we can see that the first line is
n++;
this is an increment operator, it increments the preceding value by 1, therefore n is incremented to 1

the following line will display the current iteration of the loop and the updated value of n
console.log(n);


#5

Thanks for the reply!
You are right. But in the example in the exercise:

while (currentCard !== ‘Spade’) {
//console.log(currentCard);
currentCard = cards[Math.floor(Math.random() * 4)];
};

There is no incremental function in the curly brackets. So how does it really work? :disappointed_relieved:


#6

There is no incremental function in the curly brackets. So how does it really work? :disappointed_relieved:

@ajaxpro05326 with every iteration of the loop’s code block, the currentCard changes its value, because of the Math object - this object is taking a random element or string from the card array.

with this value change - the currentCard will display either the previous or current value/string…

so in this example it is the previous iteration’s value of currentCard because we assigned a value to it beforehand - in a previous iteration

while (currentCard !== ‘Spade’) {
console.log(currentCard);
//previous value of currentCard
currentCard = cards[Math.floor(Math.random() * 4)];
};

and in this example it is the current iteration’s value of currentCard because we assigned a value to it in this iteration

while (currentCard !== ‘Spade’) {
currentCard = cards[Math.floor(Math.random() * 4)];
console.log(currentCard);
//current value of currentCard
};

ALSO in this example we have the same console.log statement but it may contain two separate values!

while (currentCard !== ‘Spade’) {
console.log(currentCard);
//previous value of currentCard
currentCard = cards[Math.floor(Math.random() * 4)];
console.log(currentCard);
//current value of currentCard
};

we use console.log() to give us an indication of what the value is at a specific point in the loop’s code block, depending on where it is placed will tell us how the values change in each iteration :smile:

using console.log() to track information and how your code behaves is a great debugging skill :+1:


#7

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.