Number Guesser for Javascript

Hey, I have just started and I have a question about the Number Guesser project.

My compareGuesses function is totally different then the solution one.
But it works. So the question is, is my solution bad way doing it?

This is the link:

Thank you for the answers!

My code:

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

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

const compareGuesses = (human, computer, secret) =>{
  if (Math.abs(human - secret) < Math.abs(computer - secret)){
    return true;
  }
  if( Math.abs(human - secret) === Math.abs(computer - secret)){
     return true;
     }
  else{
    return false;
  }
};

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

const advanceRound = () =>{
currentRoundNumber += 1;
};

Hello, @py0633377077.

Welcome to the forums!

Your function is an acceptable way of accomplishing the task. It produces the expected outcome perfectly well. It could be refactored to be significantly more concise if you have the desire to do so. Consider this example:

const compareVals = (val1, val2) => val1 <= val2 //concise body arrow function

//could also be written:
/*
const compareVals = (val1, val2) => {
  return val1 <= val2
}
*/

console.log(compareVals(5, 6)) //true
console.log(compareVals(5, 5)) //true
console.log(compareVals(5, 4)) //false

The expression val1 <= val2 is evaluated, and that result is returned. To apply this to your code, keep in mind that val1 and val2 could also be expressions. You are currently comparing expressions like this:

What happens if you do the following instead?

return Math.abs(human - secret) < Math.abs(computer - secret)

An understanding of the above will help you to reduce the need to ever return true or return false. Doing so basically looks like this:

if (true) {
  return true
} else {
  return false
}

A bit redundant wouldn’t you say?

Happy Coding!

P.S. I edited your post to format your code so that it is easier to read. For future reference, see: How do I format code in my posts?