Number Guesser

I’m stuck at extra task. It says:

  • You probably calculated the distance from the computer guess to the target and from the human guess to the target. Move this into a separate getAbsoluteDistance() function that takes two numbers and returns the distance, and then use that inside your compareGuesses() function.

This is what I have so far:

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

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

const compareGuesses = (humanGuess, computerGuess, targetGuess) => {
  const humanDifference = Math.abs(targetGuess - humanGuess)
  const computerDifference = Math.abs(targetGuess - computerGuess)
  return humanDifference <= computerDifference;

const getAbsoluteDistance = () => {

const updateScore = winner => {
  if (winner === 'human') {
  } else if (winner === 'computer') {

const advanceRound = () => currentRoundNumber++;

I have no clue how to go on. Can someone give me a hint please?

Thank you.


const humanDifference = Math.abs(targetGuess - humanGuess)
const computerDifference = Math.abs(targetGuess - computerGuess)

is duplicate code, so we should write a function which gives us the difference:

const humanDifference = getAbsoluteDistance (argument1, argument2)

Should I use humanDifference and computerDifference as arguments? Still stuck.

no, the function should calculate the difference between the target number and a guess (either computer or human), and then return the result which you can store in humanDifference or computerDifference variable:

const humanDifference = getAbsoluteDistance (argument1, argument2)
const computerDifference= getAbsoluteDistance (argument1, argument2)

A post was split to a new topic: Number guesser

Doesn’t this already calculate difference? I don’t get it.

Edit: I tried this:

const humanDifference = getAbsoluteDistance (targetGuess, humanGuess);
const computerDistance = getAbsoluteDistance (targetGuess, computerGuess);

and got error in console that getAbsoluteDistance is not defined. Shouldn’t I define it first?

Either use Math.abs, it’s fine, or make a dist function so that you don’t have to think too much about how to express distance (difference >>> abs)

const getAbsoluteDistance = Math.abs(targetGuess - humanGuess);
const getAbsoluteDistance = Math.abs(targetGuess - computerGuess);

Like this?

You’ll have to argue yourself for how it should or shouldn’t be. Think through what should happen before writing any code. Do you have a plan for what the code you wrote should be doing? Did you try it? Did it do what you expected?

If you start with considering what you have and what you need to obtain, then that says something about how the missing puzzle piece should look. You’ve got some output and input specification and you need to make them meet.

Don’t…arbitrarily paste things together.

at least define the method before you run the code, yes

yes, but its a bit duplicate, so it would be to isolate this functionality into its own method