Number Guesser

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

// Write your code below:
// computer generates random number 
const generateTarget = Math.floor(Math.random () * 10)

// compare guesses and find out the winner
const compareGuesses = (human, computer, target) => {
  let humanGuess = Math.abs(target - human)
  let computerGuess = Math.abs(target - computer)
  return userGuess <= computerGuess

  // comparing the difference 
  if(humanGuess <= computerGuess) {
    return true
  } else if (humanGuess > computerGuess) {
    return false
  }
  
}

// updating the score 
const updateScore = winner => {
  if(winner === 'human') {
    humanScore+=1
  } else if(winner === 'computer') {
    computerScore+=1
  }
} 

//Advanced Round
const advanceRound = () => 
  currentRoundNumber +=1





Hey there, my code doesn’t return any values, could you please have a look! Many thanks in advance

Hi @peterngugi0464500303
welcome to the forum!

can you provide your function call as well?

@mirja_t Thank you for pointing that out. I have made an update, but the computer keeps winning. Also says target Number undefined. Here is my code.

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

// Write your code below:
// computer generates random number 
const generateTarget = () => {
  target = Math.floor(Math.random () * 9)
}

generateTarget()



// compare guesses and find out the winner
const compareGuesses = (human, computer, target) => {
  let humanGuess = Math.abs(target - human)
  let computerGuess = Math.abs(target - computer)
  
  // comparing the difference 
  if(humanGuess <= computerGuess) {
    return true
  } else if (humanGuess > computerGuess) {
    return false
  }
  
}

compareGuesses()

// updating the score 
const updateScore = winner => {
  if(winner === 'human') {
    humanScore+=1
  } else if(winner === 'computer') {
    computerScore+=1
  }
} 

//Advanced Round
const advanceRound = () => {
  currentRoundNumber +=1
}

advanceRound()




The way you call them, your functions aren’t linked in any way. But they rely on each other. The compareGuesses function needs the result of generateTarget for example.

Here you are calling compareGuesses() with no arguments.

But it expects three arguments. One of them would be the result of generateTarget.

@mirja_t Thanks but still very much stuck. Could you give me further insight

From what I remember, there are two exercises in the JS course which let the computer and the user play against each other. One is plain JS and the other includes html and provides a preset of functions and function calls. I don’t know which of them you are referring to. Can you provide a link to the exercise?

here you go: Link

Ok, in this exercise the function calls are already written by Codecademy in game.js. So you do not have to do it. Then please specify what you mean by

It is not supposed to return values but update the view in the browser window. What happens when you play the game there? Maybe you forgot to make a guess there first? Your functions look ok to me.

The computer keeps winning! and also says Target Number undefined!

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

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




// compare guesses and find out the winner
const compareGuesses = (human, computer, target) => {
  let humanGuess = Math.abs(target - human)
  let computerGuess = Math.abs(target - computer)
  
  // comparing the difference 
  if(humanGuess <= computerGuess) {
    return true
  } else if (humanGuess > computerGuess) {
    return false
  }
  
}


// updating the score 
const updateScore = winner => {
  if(winner === 'human') {
    humanScore+=1
  } else if(winner === 'computer') {
    computerScore+=1
  }
} 

//Advanced Round
const advanceRound = () => {
  currentRoundNumber +=1
}






Did you do step 7?
What happens when you call your function compareGuesses with three arguments? Eg.

console.log(compareGuesses(2, computerScore, generateTarget()))

Put a console into compareGuesses above your return statements, too, which log your inputs:

console.log(humanGuess, computerGuess, humanGuess <= computerGuess);

I wonder if you just forgot to make a guess first by pressing the ‘+’ ?
Bildschirmfoto 2021-04-19 um 12.45.11

Thanks a lot @mirja_t have a wonderful time ahead

What was the solution exactly? Making the guess first in the browser window? It’s a bit confusing since it’s showing a number already…