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 trial
Alexander D
8,685 PointsWeekly challenge - display the last two matching numbers. Can't find a solution!
Hello there,
I have been thinking about this for a while, but I can not really find a solution. It's a simple program, nothing special. The computer creates a number, and inside a loop keeps comparing this number to a random number. If they match, the loop breaks, otherwise it keeps on spinning.
My idea is that I wanted to print either on a document, or in the consol the last matching couple. For example, the "random number initially chosen is 5 and the matching number is 5, the loop breaks" or something along these lines. I am able to print the complete sequence up until before the last number, but I can't print the number that makes the loop break.
I've tried storing those numbers in a variable, in an array and so on. Nothing worked. Any ideas?
var Random = RandomNumber ()
var array = [];
function RandomNumber () {
return Math.floor(Math.random() * 100) + 1;
}
while (Random !== RandomNumber () ) {
var message = "<p>" + Random + " " + RandomNumber () + "</p>"
document.write(message )
if (Random === RandomNumber ()) {
var message = "<p>" + Random + " " + RandomNumber () + "</p>"
}
}
As you can see I am never able to write down the last matching couple.
1 Answer
KRIS NIKOLAISEN
54,974 PointsNot sure if this is what you are looking for:
var Random = RandomNumber()
var array = [];
var r = 0;
function RandomNumber() {
return Math.floor(Math.random() * 100) + 1;
}
while (Random !== r ) {
r = RandomNumber()
var message = "<p>" + Random + " " + r + "</p>"
document.write(message)
}
Alexander D
8,685 PointsAlexander D
8,685 PointsThat's exactly what I was looking for. I think I got closer to your solution. It seems you're storing the second random number in a variable "r". If you do not do this, the trick doesn't work. Am I correct?
What is mind blowing me is that if I do the following, I create an infinite loop. "Random" and "r" are always different, therefore the loop runs forever.
Wrong code below:
Therefore I see that you are creating a change, but I can't really wrap my head around this.
I also noticed that If I use the following code, I see sometimes coming up two identical couples like 1 -1 ; 8-8 etc.. Those come in random order, even before the program ends, which I can't explain because when the computer matches those two numbers, the loop should break and according to my code they shouldn't be written there.
With your code, the matching couple is always the last string
Thanks!