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 trialEmil Pesaxov
3,684 PointsCannot understand the while loop code.
If $player1 has an initial score of 0 and $player2 has an initial score of 5, how can the while loop make an absolute value of -5 to 5 and say that it is less than 2 and say it's true? while(abs($player1 - $player2) < 2 || ( $player1 < 11 && $player2 < 11)){ }
also how exactly do the player get a score from the rand() function? It gives a number between 0 and 1. If it's 1, player 1 gets a score and if 0 player 2?
1 Answer
Emil Pesaxov
3,684 PointsOh ok, I think I got it the second time I watched the video, lol.
The rand(0,1) means that the min and max values are inclusive. So each time the function will randomly generate either 0 or 1. Since 0 is false and 1 is true, $player1 will receive a point when 1 is generated since he is an the if code block and player 2 will receive a point when 0 is generated since he is in the else code block.
Regarding the second issue, as long as the difference between their scores is less than 2 we will continue to loop. Since Alena gave $player2 = 5 and we find the absolute value which gives 5 < 2 it evaluates to false but the other condition is still true as they are less than 11 and the loop continues to loop. I hope I got it right.