1

My lunch time with Javascript tutorial on codecadamy.com.

I have this simple loop:

var cards = ['Diamond', 'Spade', 'Heart', 'Club'];
var currentCard = 'Heart';

while (currentCard !== 'Spade') {
  console.log(currentCard);

  var randomNumber = Math.floor(Math.random() * 4);

  currentCard = cards[randomNumber];
}

console.log('Found a Spade!');

This goes fine, but if I remove randomNumber from loop and place with other global variables, codecademy console doesn't print the result...

Since that global variables should be available to that loop, I assume that something is wrong with codecademy console?

Do you agree that this supposed to work?

var cards = ['Diamond', 'Spade', 'Heart', 'Club'];
var currentCard = 'Heart';
var randomNumber = Math.floor(Math.random() * 4);

while (currentCard !== 'Spade') {
  console.log(currentCard);
  currentCard = cards[randomNumber];
}

console.log('Found a Spade!');

2
  • instead of generating randomnumber use variable i=0 and increment it. Commented Nov 14, 2016 at 10:44
  • Thanks. I hope next curse will tell me to do that :) Commented Nov 14, 2016 at 11:05

4 Answers 4

5

By moving the random number outside of your loop it means it is only evaluated once.

So the first time the loop runs, if the random number ends up as any value other than 1, the while loop will run infinitely, because currentCard will always not be equal to 'Spade'.

So what is happening is that the codeacademy console will be evaluationg that loop infinitely, and probably end up crashing.

Sign up to request clarification or add additional context in comments.

2 Comments

great explanation. Thanks! Now it looks so obvious... Maybe my brain is not loop-thinking ready :D
No problem - I'm sure your brain is loop-able given some practice! :)
4

Its because that your code goes into the infinite loop.

Since in your code random number is generated only once. and there is possibility that currentCard !== 'Spade' becomes true always, and it run into infinite loop/

3 Comments

Oh, thats interesting. Thanks, I will think about it
it only works when it doesn't get in the while loop.
@KevinKloet Yes, It means that when the currentCard is assigned with "Spade".
2

The best way to check it: use browser console.

ctrl(cmd)+shift+i 

End just paste your code and check it

2 Comments

sorry, cmd+option+i on mac
But on your example random number will be one for every iteration of your loop
1

The second sample code you provide has a problem due to the fact that the randomNumber variable is assigned only once. The variable is passed by value to your loop, not by reference, meaning it will not change while inside the loop.

This means that any value assigned to your randomNumber variable will remain the same until changed somewhere else in your code. Consider the following examples:

Example 1

var cards = ['Diamond', 'Spade', 'Heart', 'Club'];
var currentCard = 'Heart';
var randomNumber = Math.floor(Math.random() * 4);

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

console.log('Found a Spade!');

In this case, the variable is global, but its value changes in every iteration, just like it does in the tutorial you are following. The only difference is the scope of the variable.

Example 2

var cards = ['Diamond', 'Spade', 'Heart', 'Club'];
var currentCard = 'Heart';

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

console.log('Found a Spade!');

In this case you do not use a variable at all, just the result of the functon. This is not the kind of solution you were trying to implement, it just serves to show you how you can use a function's result and change it at every iteration of your loop.

To sum it all up:

The randomNumber variable does not keep a reference to the function called, so its value remains unchanged no matter how many times you access it in the loop. If you want it to be global, you can change its value inside the loop, as shown in Example 1. You can use the output of the function as-is, without assignment to a variable, using the method shown in Example 2.

1 Comment

Deep explanation. I appreciate it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.