Codecademy Forums

Number guesser help

Some things I noticed:

compareGuesses function expects a secretNumber as 3rd input parameter yet you redefine it inside of the function. It should work, but what is your motivation to do so?

There are no function calls, so how would you know it works? All I can see are function definitions.

i defined secretNumber as my generateTarget function

i modified the code like this:

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 {
console.log(‘You win’)
}
}

and it only did the console.log and did not return the ‘true’ or ‘false’

Is your function supposed to return a result or print something out? Seems to do different things for different results, that would be very confusing to use.

probably not:

if this
  then return something
  else print something

instead:

if this
  then return something
  else return something

i just thought of something… if the else in the function above is returning and the other ones are not then that must mean that there is something wrong with the code arrangement or the code itself

You have three outcomes.
Two of them return something.
The third prints something.
Returning and printing are completely different actions, and your function should probably present the result the same way regardless of what the result is.

i did that code as a test to see how to properly return it

i changed my code to return all three of the things but it is still not showing up:

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

is it because i am returning boolean values? is that the right way to return something like this?

What does showing up mean though?
If you mean being printed, then, you’d have to print it. Printing doesn’t happen spontaneously, it’s something that the code would have to contain instructions for.

i mean showing up on the number guesser application

And there exists a function for showing things. You’d use that for any values that you want to display.

what is this function? i genuinely do not know what the return command is for the specific type of terminal the number guesser is on.

A function gets input through its parameters, and outputs with return. Some value(s) go in, and something goes out.

Printing is unrelated to that. Most functions shouldn’t be printing anything, they should do something with the input received, and return a result.

It’s probably the first thing you got introduced to. A hello world program uses it to write something to screen.

// computes the sum of two values
function add(a, b) {
  return a + b
}
// doesn't compute anything at all. but it has a side-effect:
// it displays something to screen
function say(message) {
  console.log(message)
}

Somehow I have the feeling you don’t quite understand the difference between variables and input parameters. If you do understand it, please don’t feel insulted by my explanation.

A variable is simply a vessel to store a value as in:

let myAge = 31;

An input parameter is a parameter that receives an initial value passed upon calling of a function. Lets say we want to write a function that tells how old someone is in 17 years. We would have to define a function that requires one input being how old someone is now.

function howOldInSeventeenYears (currentAge) {
     return currentAge + 17; // This returns a value of 17 added to the currentAge
};

Now lets call it with my age by injecting myAge as the currentAge input.

howOldInSeventeenYears(myAge); 

This function call will run the function, but we haven’t told the console to show this value. So we have no clue what the outcome is. So in order to see what the outcome is we have to either, A, call the function inside a console.log() or B, modify the function to print the outcome, the last one still requires a function call.

// option A
console.log(howOldInSeventeenYears(myAge)); // this logs the outcome of the function call

// option B
function howOldInSeventeenYears (currentAge) {
     console.log(currentAge + 17); // This logs the value of 17 added to the currentAge to the console
}

// but we still need to call for the function for the logging to actually happen.
howOldInSeventeenYears(myAge);  // calls the function which then logs the outcome

So now back to your code I have some remarks:

I am not seeing you call the compareGuesses function anywhere. If you don’t call it, it will not run.

I am not seeing any request to console.log the outcome (option A) in your function let alone option B because you are not calling the function anywhere.

And last but not least you have secretNumber as your third input parameter, yet you reassign it immediately after with a variable new to me. Where is this variable coming from? Is there a reason for doing this? This has me confused bigtime.

Anyways, I hope I could be of any assistance with this explanation and my remarks, and I am curious on how you are going to solve this and log the outcome of the guessing game.

1 Like

Hi there,
I just finished this project. My code is very similar to yours, and it works, so maybe it is something with the rest of the code? Or maybe you haven’t saved it properly? Are you doing it on your computer or Codecademy?
And about calling the functions, instruction says: " We’ve provided some additional JavaScript code in game.js that will call your functions based on user interactions, but you don’t need to look at game.js and shouldn’t edit it if you want your project to work as intended. As you complete this project, make sure that all of your functions are named exactly as specified so that they can be called correctly when the game is played."

Which means that you didn’t do anything wrong by not calling any functions or not printing any message - it’s already there in game.js.
Hope this was helpful :wink:

3 Likes

I just looked closely at this thread for the first time after seeing @runaway.cat’s post . Seems a link to the project would have saved a lot of confusion. Here’s the link: https://www.codecademy.com/practice/projects/number-guesser-independent-practice

Most of the advice you’ve received, while sound, doesn’t apply to the project. As @runaway.cat pointed out, the script.js file should only contain the functions that we are instructed to code in the project steps. The games.js file contains the function calls, runs the game, etc. The functions in the script.js file don’t need to print anything to the console via console.log. They only return values to their callers. Hopefully you haven’t given up on this one. If you’re still working at it, feel free to post your current code, and with the aid of the link to the lesson, I’m sure you’ll receive the help you need to get this project working as expected. :slightly_smiling_face:

2 Likes

where is the games.js file? i don’t know where it is

You can open file explorer by clicking this button ->

yes… and where do i go from there