Codecademy Forums

Number guesser help

my compareGuesses function is not showing up on the number guesser, even though I got all the syntax correct. the you win message is not going on the number guesser. what am i doing wrong?

here is my code:

function compareGuesses(humanGuess, computerGuess, secretNumber) {
secretNumber = generateTarget();
var userDistance = Math.abs(humanGuess - secretNumber)
var computerDistance = Math.abs(computerGuess - secretNumber)
if (userDistance < computerDistance) {
return false;
} else if (userDistance > computerDistance) {
return true;
} else {

}
}

Should that be a call to computerGuess()?

Consider, though, since both the target and computer are generated by essentially the same function,

var computerDistance = Math.abs(generateTarget() - secretNumber)

what do you mean by call?

parameters are different from arguments, you cant call them

i was trying to get the absolute value since that is what this project requires

We don’t see your entire code, so are just guessing.

that part of the code is the only part i am having a problem with

Copying and running the code results in syntax error.
So in order to get the code you’re really asking about, there’s guessing involved.
Gotta enable others to see the same thing.

Oh, after glancing at it, maybe it doesn’t, still, since the forum is allowed to apply formatting rules to it, I don’t trust it. If there’s any other code involved required to reproduce what you describe, then that’s still missing.

here is the full code :

function generateTarget() {
randomTarget = Math.floor(Math.random()* 10);
return randomTarget;
}

function compareGuesses(humanGuess, computerGuess, secretNumber) {
secretNumber = generateTarget();
var userDistance = Math.abs(humanGuess - secretNumber)
var computerDistance = Math.abs(computerGuess - secretNumber)
if (userDistance < computerDistance) {
return false;
} else if (userDistance > computerDistance) {
return true;
} else {

}
}

There are functions there but they’re never called, so nothing should happen, right?

for some reason running this did not give a syntax error

do you mean i am calling the functions wrong?

There’s no code for that. So there’s no reason why it would happen.

how do i implement the code for that? i tried a lot of stuff and none of it worked

console.log('you win')

… what are you asking exactly >_<

I don’t know what you’re wondering.

But if you want some kind of result from your function, then you’d probably call it, and then print out the result? And to call it you would first need input, so you’d define some input, call it, print the result.

the strange thing is i tried a separate function to put out ‘you win’ and ‘you lose’ but that did not work

What does work mean?
Maybe that’s where you’d continue. What was different from what you expected?
In some sense programming is about lining up events to get from where you start to where you want to be. It is to take control of every detail from start to finish. If it’s not working then the way to continue is to start comparing what should have happened to what does happen.

Perhaps you might start with writing a hello world program that does nothing but print out a greeting, and modify that a small piece at a time until you get to the result that you’re looking for?

If the problem is that you’re not getting anything to show on the screen then a hello world program is a good starting point since it does exactly that. From there you’d modify it to print out a different message based on the result of your function, not a big change, but that small change is probably already puts it very close to whatever you’re expecting to happen.

oh… thanks for the advice!

Getting anywhere with that?
If you know how to write a hello world program, and you know how to write if-statements, and you know how to call your function, and you know how to define input for your function… Then is it reasonable to say that you also know how to get some kind of result from your function onto the screen? If not, and you very well might not, then maybe you’re able to say which of those things (or any other thing that stopped you) you don’t know how to do, and that’s a whole lot of progress, because then you know what information you’re missing.