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 trialQuinton Dobbs
5,149 PointsWouldn't (parseInt(Guess) !== randomNumber) have the same effect as (! correctGuess)?
Then you wouldn't have to create a "correctGuess" variable or create a conditional statement if I am correct?
2 Answers
Steven Parker
231,269 PointsYes, you could make that substitution.
And in this simple example that would work and make the code more compact. I suspect it was shown the other way just to demonstrate a common technique of carrying the result of a comparison in a boolean, even thought it isn't really needed in this particular situation.
Another possible substitution (halfway between yours and the original in efficiency) is to replace the entire if block with this:
correctGuess = parseInt(Guess) === randomNumber;
nfs
35,526 PointsMy short experience of programming in javascript ( roughly 259200 seconds ) assures me that there are several ways of writing the same piece of a programme. Am I right??
Steven Parker
231,269 PointsThe bigger the program, the more ways there are of writing it.
Ana Luiza Barreto Marinho
2,210 PointsYup! Each one finds the solution the best fits the necessity at the time :). There are a few things you should count for such as the amount of memory your code is going to take and the time it will take to execute, but don't worry about those things now ^^.
Quinton Dobbs
5,149 PointsQuinton Dobbs
5,149 PointsI see, thank you. I suppose there may be a few situations that I haven't learned about yet that may prefer the (! correctGuess) method.