Number Guesser Project

Project link:
https://www.codecademy.com/practice/projects/number-guesser-independent-practice

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

// Write your code below:
const generateTarget = () => {
  return Math.floor(Math.random() * 10);
}

const target = generateTarget();

const compareGuesses = (user, cpu, target) => {
  const diffU = Math.abs(target - user)
  const diffCpu = Math.abs(cpu - target)
    
  if (diffU <= diffCpu) {
    let winner = 'human'
    return true;
  } else {
    let winner = 'computer'
    return false;
  }
}

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

function advanceRound() {
  currentRoundNumber ++;
}

generateTarget();
compareGuesses();
updateScore();
advanceRound();

I’m trying to understand why my game won’t work. Can I get some help please?

expand this

if you watch someone cook a lasagna but end up with pizza, how would you find out what went wrong? observe what happened and compare to what you think should have happened. some thing here. expand “wrong” and compare to what you meant.

I expected that the number guesser game on the right would function; take my guess, compare it with the computer, declare the winner, and move on to the next round. However, after calling my functions, the game is still stagnant. So, in that sense, what did I do wrong?

that’s a synonym to “work”
those words avoid saying what they mean! you can’t work with that.

You need something that you can observe, measure.

Something that might be a good idea to look at is what inputs are provided to your functions and how they respond. In particular you’d want to look at this for situations where you perceive something to behave incorrectly. Similarly, the functionality that isn’t doing what it should also likely says something about which part of your code is responsible, the one(s) related to that feature.

This is your measuring tool: console.log (the results will show up in the console of your browser, because that is where the page is displayed) there’s also nothing stopping you from making measurements from the other javascript file.

I clarified what I expect to happen when I said after the semicolon, “take my guess, compare it with the computer, declare the winner, and move on to the next round”. So as of now, my program isn’t producing these results. I want to know what I’m missing or miscoding. Thanks in advance.

You misunderstand. You’re the one who should find that out. Whatever code you write should be something that you’re also able to fix when things go wrong.
And you’d do that by identifying a behaviour that you think should be different, and making measurements on what happened to obtain that behaviour so that you can compare to what you thought should have happened.
That’s what the other 70% of my post was about. Well, 100 really, because you need some event to study first and that’s the thing that’s “wrong”.

I’m sorry but I really don’t understand what you’re saying. I posted my code right at the top and I posted the link to the project which explains exactly how the completed game should work. All I want is for someone to point out where in that snippet I went wrong. It’s pretty clear what I’m asking I should hope

Hello, @chip2351237649.

When I use your code instead of mine in the project with the browser console open as @ionatan has suggested, I see the following error:

This error prevents any of the remaining code from running. I was able to get your code to run as expected only by deleting the things that don’t belong. If you’ll re-read the instructions, you’ll see that you only need to write 4 functions. The code in game.js will call your functions. See if you can determine the cause of the error shown. Feel free to continue replying as you need more assistance.

I started again, and now my number guesser works normally. It appears the point about keeping your function names coherent was what I was missing. Thank you very much

The issue was you’re calling of the functions. The game.js file contains all of the code for calling the functions. When const target = generateTarget(); was executed. It called the generateTarget() function, and created a global constant variable named target. When the code in game.js then tried to call your generateTarget() function and assign the value returned to a variable named target using let, an error was thrown because the constant target cannot be redeclared.

Edit: Simply calling the functions from script.js wouldn’t prevent the game from being played, but could potentially mess up the expected behavior of the game.

The error message in the console shows that game.js does not execute to end, doesn’t do anything at all, actually. So none of the triggers exist.
Both game.js and script.js are executed in the same scope. They are both trying to declare a variable by the same name, which is where game.js crashes.

This topic was automatically closed 18 hours after the last reply. New replies are no longer allowed.