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

JavaScript JavaScript Loops, Arrays and Objects Simplify Repetitive Tasks with Loops `do ... while` Loops

Quinton Dobbs
Quinton Dobbs
5,149 Points

Wouldn'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
Steven Parker
229,744 Points

Yes, 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;
Quinton Dobbs
Quinton Dobbs
5,149 Points

I see, thank you. I suppose there may be a few situations that I haven't learned about yet that may prefer the (! correctGuess) method.

My 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
Steven Parker
229,744 Points

The bigger the program, the more ways there are of writing it.

Ana Luiza Barreto Marinho
Ana Luiza Barreto Marinho
2,210 Points

Yup! 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 ^^.