Number Guesser Challenge Project (JavaScript)

Hello Everyone! this is my project!

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

// Write your code below:
const generateTarget = function(){
  let randomNumber = Math.floor(Math.random() * 10);

  return randomNumber;
}

function compareGuesses(userGuess, computerGuess, secretGuess){
  if(Math.abs(secretGuess - userGuess) < Math.abs(secretGuess - computerGuess) || Math.abs(secretGuess - userGuess) === Math.abs(secretGuess - computerGuess)){
    return true
  } else{
    return false
  }
}

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

function advanceRound() {
   currentRoundNumber =  currentRoundNumber + 1;
}

This went a little easier than expected. The one part that did trip me up a little was ensuring that when testing the guesses my result was always a positive number. I almost went down the rabbit hole of if else statements.
Link to Github: number-guesser-starting/README.md at main · Robert-Gueydan/number-guesser-starting · GitHub

https://robert-gueydan.github.io/number-guesser-starting/

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

// Write your code below:

//Generate secret number
const generateTarget = () => {
    return Math.floor(Math.random() * 10);
  }
  
//compare computer vs human guess to the secret number and choose a winner  
const compareGuesses = (humanGuess, computerGuess, secretNumber) => {
  if (humanGuess > 10){
    alert('Choose a number between 1-10 Next time');
  }
    let computerCompare = Math.abs(secretNumber - computerGuess); //returns the difference as an absolute number
    let humanCompare = Math.abs(secretNumber - humanGuess);
      if ((humanCompare === computerCompare) || (humanCompare < computerCompare)) {
        return true;
      } else {
        return false;
      }
  }

  //updates score for each round
  const updateScore = winner => {
      if (winner === 'human') {
          humanScore ++;
      } else {
          computerScore ++;
      }
  }

  //advances rounds
  const advanceRound = () => {
    currentRoundNumber ++;
  }

2 Likes

Hi everyone! New to the forum but hope I’m posting in the correct place. I am have some difficulties in understanding parts of the Number Guesser challenge. More specifically this part of the solution (task 2):

const compareGuesses = (humanGuess, computerGuess, target) => {
  const humanDiff = Math.abs(target - humanGuess);
  const computerDiff = Math.abs(target - computerGuess);
  if (humanDiff <= computerDiff) {
    return true;
  } else {
    return false;
  }
}

I had to look at the solution because I was stuck trying to specify the value of ‘target’ which does not seem to be necessary according to the solution? I completed the rest of the tasks but it bothers me that I don’t understand this part.

Thanks in advance,
Max

The value of target is specified in the other JavaScript file, game.js

so you just have to make a function to compare it to the user’s guess and computer’s guess and indicates which one’s closer (by returning true or false).
This function is called in game.js too.

Ok got it! Appreciate the response!

Hi everyone, beginner programmer here!

Below is my solution for this project, if anyone has time to give me some feedback I would really appreciate it. Thank you!

https://gist.github.com/b8e2a3abc750d92e5ae6ad9b1fa90df8

Hi All,

Here is my number guessing game.

https://lkkiselov.github.io/nuber-guessing-game/

Thank you for your time!

If you wish me to review your exercise please post in reply below!

LK

The solution
let humanScore = 0;

let computerScore = 0;

let currentRoundNumber = 1;

// Write your code below:

//function to get random generated number.

function generateTarget(){

let targetGuess = Math.floor(Math.random() * 10);

return targetGuess;

}

//comparing the guesses to the target gues to see which on was closest.

function compareGuesses(humanGuess, computerGuess, targetGuess){

const humanD = Math.abs(targetGuess - humanGuess);

const computerD = Math.abs(targetGuess - computerGuess);

if (humanGuess === targetGuess || computerGuess === targetGuess){

    //human player wins

    return true;

}

if (humanD < computerD){

    //human player wins

    return true;

}

if (computerD < humanD){

    //computer player wins

    return false;

}

}

//updating the score using a function

function updateScore(winner){

if (winner === 'human'){

    humanScore += 1;

}else{

    computerScore += 1;

}

}

//function for increasing the round number of the game.

function advanceRound(){

currentRoundNumber += 1;

}