Number Guesser Project

I don’t understand why my code isn’t working properly.

let humanScore = 0;
let computerScore = 0;
let currentRoundNumber = 1;

// Write your code below:

const generateTarget = () => {
  let randomNumber = Math.floor(Math.random() * 10);

  return randomNumber;
};

const getAbsoluteDistance = (num1, num2) => Math.abs(num1 - num2);

const compareGuesses = (userGuess, compGuess, secretNum) => {
  if (getAbsoluteDistance(userGuess, secretNum) <= getAbsoluteDistance(compGuess, secretNum)){
    return true;
  } else if(getAbsoluteDistance(userGuess, secretNum) > getAbsoluteDistance(compGuess, secretNum)){
    return false;
  }

  //(getAbsoluteDistance(userGuess, secretNum) <= getAbsoluteDistance(userGuess, secretNum)) ? true : false;
};

const updateScore = winner => {
  if(winner === 'human'){
    humanScore++;
  } else if (winner === 'computer'){
    compurerScore++;
  }
};

const advanceRound = () => currentRoundNumber++;

Can you elaborate on how, precisely, you think it’s not working properly?

1 Like

yeah, so what the program is supposed to do is accept a number from the user and the computer is supposed to give a number and compare both to a number to a target number and see which one is closest. But for some reason it only says ‘You win’ and it never says ‘Computer wins’. So if the computer wins, you’ll do it a couple of times until it says ‘You win’ (thats the only result for some reason). Are you familiar with Number Guesser Codecademy project?

Because Codecademy gives you the rest of the files needed for the project and you just have to make those functions that i attempted.

Thanks for responding

Have you tested your code? You don’t need their code to provide input, you can just as well do that yourself.
Don’t leave it at “some reason” - take a look.

I tested the function compareGuesses which was the one i thought wasn’t working and it did return the proper values, could you tell me what’s wrong?
And i did check for bugs and everything, but since it wasn’t returning the desired outcome with the project files thay gave me i assumed my functions weren’t working. So could you please help me?

Well. What would you want to use as input?
Ideally the same thing as you’re perceiving as not working, you’d want to make measurements on the input and output of your code.

That way you can tell what the inputs are, and what the outputs are, you can manually judge whether those are correct.

So you’d print them. Right? That is how we measure things. You could modify the page as well, but printing is a bit more straight-forward

console.log({userGuess, compGuess, secretNum})

(keep in mind this runs client-side (it’s a website), so it will appear in the console of your browser)

They provide the other files as well, letting you read or edit them.

You’ll probably notice something rather disturbing when you do this, you’re not receiving all numbers. This is a bug in the code codecademy provides. However, this doesn’t account for the missing victory message, and it turns out this bug cancels out in a two wrongs make a right fashion.

Another thing happens when the computer’s victory message should have shown up. An error message is written to the console (again, the browser’s console, since that is where your code runs). Something crashed. The error message says what and where.

Yeah, thanks. I had a bug.
instead of it being

const updateScore = winner => {
  if(winner === 'human'){
    humanScore++;
  } else if (winner === 'computer'){
    computerScore++;
  }
};

it was like this

const updateScore = winner => {
  if(winner === 'human'){
    humanScore++;
  } else if (winner === 'computer'){
    compurerScore++;
  }
};

So i guess that’s why it never said the computer won, because codecademy’s code was tied to that functionality.

Thanks.

P.S. I printing the input,

console.log({userGuess, compGuess, secretNum})

and as i expected it said ’ userGuess not defined’ , am i doing it wrong. And for some reason when i refresh my file in my browser it says ‘ERR_FILE_NOT_FOUND’ do you know why? ( I didn’t move it).

ok, so i was trying some things and when i refreshed my page it didnt happen but when i went to the link and pressed enter it did, and the url changed from ‘file:///C:/Users/f/OneDrive/Documents/Coding%20Projects/numberGuesser/index.html’
to
‘file:///C:/Users/f/OneDrive/Documentos/Coding%2520Projects/numberGuesser/index.html’

i saw an answer on stack overflow but didn’t really get it

Idk what you did or what you have.