Number Guesser Challenge

Hello everyone!

I’m at the end of intro to JavaScript section, it’s been a lot of fun and had a few moments of challenges thus far, but I’m pressing forward and trying to soak up all the knowledge I can before I start my web/software dev coursework in the fall. I currently have a good understanding of HTML/CSS and built some static sites, and after getting through the intro to JS section here at Codecademy, was asked to complete the Number Guesser Challenge Project and have faced some difficulty in the early going of the project that I could use some help on.

const generateTarget = () => {

let randomNumber = Math.floor(Math.random() * 10);

return randomNumber;

}

const compareGuesses = (userGuess , computerGuess, secretActualNumber) => {

}

My goal is to compare the userGuess with the secretActualNumber, as well as the computerGuess with the actualSecretNumber, and return the one that is closest to it.

Based on reading some of the other questions on this forum regarding this project, and the hint section, my idea was to take Math.abs (secretActualNumber) - Math.abs(userGuess) = ?

Then, to compare it to Math.abs (secretActualNumber) - Math.abs (computerGuess) = ?

After I can see which one is lower, then I would have my answer, correct?

It seems like my syntax, or some technical error is making it come out incorrectly here. I’ve been struggling with it for a while, so I would love to hear some of your thoughts on how I might solve this issue.

Thanks in advance

Are you sure you want the absolute value of each of those individual numbers? Consider:

> Math.abs(4) - Math.abs(9)
-5
//versus
> Math.abs(4 - 9)
5
1 Like

Okay, so essentially, I should be putting them in the same Math.abs as you showed.

Syntax wise, would this work?

Based on my (limited) understanding, I don’t see error in this one.

Math.abs (userGuess - secretActualNumber)

Math.abs (computerGuess - secretActualNumber)

After this is completed, my question was how would I compare these two, in order to spit out a true/false.

Based on previous projects, it would appear that I need to call each of these a variable, like so

let userMinusActual = Math.abs(userGuess - actualSecretNumber);
let computerMinusActual = Math.abs(computerGuess - actualSecretNumber);

Thank you for your speedy reply. I really appreciate it.

Is this something you can carry out manually? I give you two numbers, how near to each other are they?
The answer is almost certainly yes, and, that means you already know what to do. Just because there’s code involved doesn’t mean your pre-existing knowledge/logic doesn’t apply.

After having decided what to do you may find it a whole lot easier to write code for it. You can’t write code for what you haven’t decided what it is, that’s what’s causing you trouble, that is quite obviously difficult.

If you describe the action in english, would you then be able to say whether that is a good strategy?

again, how would you do it manually? what comparison would you be making?

whether that is the case is testable by trying each. also, can you motivate why it would be needed? if you can’t back something up with reason then there should be very low confidence for it.

I would say yes, it can be carried out manually. If I was given two numbers, I’d find the difference between them.

I would want to compare the two values by subtracting the smaller value from the larger value.

It appears that finding the difference can be done by using the “difference” function. However, up to this point in projects, that function hasn’t been taught, so I’ve kind of been stumbling around trying to use it in a few different ways unsuccessfully.