My lunch time with Javascript tutorial on codecadamy.
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!');
My lunch time with Javascript tutorial on codecadamy..
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!');
Share
Improve this question
edited Nov 14, 2016 at 11:03
Angelos Chalaris
6,7278 gold badges53 silver badges81 bronze badges
asked Nov 14, 2016 at 10:42
Julius DzidzevičiusJulius Dzidzevičius
11k11 gold badges41 silver badges84 bronze badges
2
- instead of generating randomnumber use variable i=0 and increment it. – M14 Commented Nov 14, 2016 at 10:44
- Thanks. I hope next curse will tell me to do that :) – Julius Dzidzevičius Commented Nov 14, 2016 at 11:05
4 Answers
Reset to default 5By 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.
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'
bees true always, and it run into infinite loop/
The best way to check it: use browser console.
ctrl(cmd)+shift+i
End just paste your code and check it
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.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744795099a4594159.html
评论列表(0条)