Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialAlexander D
8,685 PointsComputer vs. Computer. Help to understand a simple function.
Hello there,
I am coming back to one topic I never fully understood. I present you with a simple function that I believe was introduced in one of the first courses with Dave about Javascript. Everything started because I wanted to be able to see the last matching couple before the program ends:
var Random = RandomNumber()
function RandomNumber() {
return Math.floor(Math.random() * 10) + 1;
}
while (Random !== RandomNumber() ) {
console.log(Random + " " + "and" + " " + RandomNumber() )
}
This function runs until two numbers match (or at least it should). One number is stored in a variable "Random", the other one changes at every iteration.
In theory, when the two numbers match, the function stops working. However, if you run the console, it's frequent to see numbers like:
9-9 9-3 9-7 9-9
Here there's a screenshot:
https://drive.google.com/open?id=1UChDUJnKDyJ2JDbWobtl-hgSM_ijZyqj
Isn't the code supposed to stop working when there's a match? Why am I presented with multiple rows of matching numbers?
Someone kind enough, responded to me with this code:
var Random = RandomNumber()
var r = 0;
function RandomNumber() {
return Math.floor(Math.random() * 10) + 1;
}
while (Random !== r ) {
r = RandomNumber()
console.log(Random + " " + "and" + " " + r )
}
If you run this code, you can see how last row in the console it's always a matching couple and there aren't rows of repeated elements. After there's a match, the code stops working.
Could you explain what is happening with the first version above, and how this version is different?
Try out the code here:
https://www.webtoolkitonline.com/javascript-tester.html
Thanks, Alex
3 Answers
Fernando Boza
25,384 PointsHi Alex, i ran both scripts and they both work, they stop running when both numbers are the same x == x. The only difference between your script and the script someone helped you with is a temporary variables called "r" which you just swap out the RandomNumber() in the while loop.
var Random = RandomNumber()
var r = 0; // temp variable initialized
function RandomNumber() {
return Math.floor(Math.random() * 10) + 1;
}
while (Random !== r ) { //first iteration is "Random is not 0 "
r = RandomNumber() // then we reassign the temp "r" and give it a randomNumber() that you would have originally called in the previous line.
console.log(Random + " " + "and" + " " + r )
}
Steven Parker
229,644 PointsThe difference is that the first code calls the random number function twice in the loop:
while ( Random !== RandomNumber() ) { // compare to a random number
console.log(Random + " " + "and" + " " + RandomNumber() ) // but print a DIFFERENT one
}
So it will stop when the number matches in the comparison, but that's not the number that is logged.
The second example calls the function only once in the loop, so it compares and logs the same value.
Alexander D
8,685 PointsThanks a lot Steven Parker ! That's very clear! I just have one last question. Why I don't get the same results (similar to my very first code) when I use the following code? (setting "Random" > r )
while (Random > r ) {
r = RandomNumber()
alert(Random + " " + "and" + " " + r )
}
Steven Parker
229,644 PointsBesides calling the function only once, another obvious difference is the original loops until the value matches, but this one loops while the value is less than the original pick.
Alexander D
8,685 PointsFernando Boza I do understand more both snippets, thanks for taking the time to respond!
That is certainly weird, I ran another test, using the first code with this program:
https://www.webtoolkitonline.com/javascript-tester.html
I do not see the matching couple being the last value (I also see "7 and 7" repeated twice")
https://drive.google.com/open?id=1DQRemUUvrx-jVTF7mMHFOAbgS5Ap7auO
However, the result is totally different with the program you're using, therefore I trust it more.
Thanks!
Alexander D
8,685 PointsAlexander D
8,685 PointsHello Fernando Boza . First of all, thanks for taking the time to read my long article.
I tried using your link and it gives a total different result than the regular console log. That's was throwing me off, and I don't understand why this is possible.
Thank you so much!
Fernando Boza
25,384 PointsFernando Boza
25,384 PointsAlexander D thats so weird, I tried both codes in the Chrome Dev console and it works. But finish running with the random numbers are equivalent. Did you understand the difference between the both snippets?