Number Guesser Help

It seems like you need to grab the COMPUTER GUESS and PLAYER GUESS in order to create a function that would be able to evaluate the difference between the target number and thePLAYER GUESS but also the target number and the COMPUTER GUESS.

How do you grab the COMPUTER GUESS. how do you grab the PLAYER GUESS?

Those functions are already written in game.js …I’m guessing.

what am I missing here?

Please post a link to the exercise page. Thanks.

  • generate a secret number
  • get a computer guess
  • get a user guess
  • compute distance from target for computer
  • compute distance from target for user
  • compare which absolute distance is smaller
  • return a winner

const absDiff = (a, b) => Math.abs(b - a)

That is a utility function that can help us cut down on code, since it is repeated.

const generate = (low, high) => Math.random() * (high - low) + low

The above is a utility function to generate a random float within a specified range (lower bound, upper bound). We have left off any change to that return value.

const floor = x => Math.floor(x)

With the above two, we can now write a function to generate integers.

const randint = x => floor(generate(1, 10))
  • target = randint(1, 10)
  • computer = randint(1, 10)
  • user = randint(1, 10)
  • winner = absDiff(user, target) < absDiff(computer, target) ? ‘User wins!’ : ‘Computer wins.’

This is not solution code as much as something to think about and tinker with. Use it for a learning tool.

Thank you.

But it seems like the PLAYER GUESS is already being generated in the web page when the project starts.

to get

const absDiff = (a, b)

then “a” in (a,b) would be the PLAYER GUESS that’s already being generated when the project starts.

Math.abs(b - a)

a or b must equal PLAYER GUESS while the other equals the TARGETNUMBER. Because you are subtracting the PLAYER GUESS from the TARGETNUMBER. I’ve created the TARGETNUMBER but I need to grab the PLAYER GUESS that is being generated when the project starts so I can complete the calculation. I need the PLAYERGUESS so I can calculate the difference in PLAYERGUESS and TARGETNUMBER.

We supply that information in the call argument…

c = absDiff(playerGuess, target))

I’m still a little at a disadvantage without access to the exercise page. Everything above is conjecture on various premises. To contribute to the actual problem, I’ll need to see the game.js file. Please post a link to that page in a reply.

let target = randint(1, 10)
  absDiff(randint(1, 10), target) < absDiff(randint(1, 10), target)
    ? 'User wins!' : 'Computer wins.'

Thanks here’s the link.

Oh, now I remember the project.

Below is a spoiler of how I handled the first six steps…

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

// Write your code below:
const floor = Math.floor;
const random = Math.random;
const abs = Math.abs;
const generateTarget = () => floor(random() * 10);
const compareGuesses = (h, c, t) => abs(h - t) <= abs(c - t)
const updateScore = w => {
  switch (w) {
    case 'computer': computerScore++; break;
    default: humanScore++;
const advanceRound = () => {

We should note that all user inputs come from the browser’s input control and is guided by event handlers. Our code in script.js is only a small part of the larger program.

We can complete the first part of step 7 with,

const getAbsoluteDistance = (a, b) => Math.abs(b - a)
//const compareGuesses = (h, c, t) => abs(h - t) <= abs(c - t)
const compareGuesses = (h, c, t) => getAbsoluteDistance(h, t) <= getAbsoluteDistance(c, t)