Number Guesser Challenge Project (JavaScript)

Here is my code
numberGuess (github.com)

Hello, I’m stuck and can’t seem to figure out where I’m going wrong. Here is my code.

Thanks.

Hey there! This is my code.

It looks like you called the updateScore function,
but you should have written an updateScore function instead.

1 Like

Here’s my code for the challenge project. You are welcome to write down your invaluable feedback :pray: :blush:

let humanScore = 0;

let computerScore = 0;

let currentRoundNumber = 1;

// Write your code below:

function generateTarget(){

return Math.floor(Math.random() * 10);

}

//calculates the absolute value of distance between the secret target number and the guess.

function getAbsoluteDistance(d2, d1){

return Math.abs(d2 - d1)

}

function compareGuesses(humanGuess, computerGuess, secretTargetNumber){

const humanDif = getAbsoluteDistance(secretTargetNumber, humanGuess);

const computerDif = getAbsoluteDistance(secretTargetNumber, computerGuess);

if(humanGuess >= 0 && humanGuess <= 9){

    if(humanDif <= computerDif){

        return true;

    } else {

        return false;

    }

} else {

  return `Set a number in the range of 0 to 9`

}

}

function updateScore(winner){

if(winner === ‘human’){

humanScore ++;

} else {

computerScore ++;

}

}

function advanceRound(){

currentRoundNumber ++;

}

https://discuss.codecademy.com/t/number-guesser-challenge-project-javascript/462394?u=digital6644712806

i try try to share my code on github but i don’have the share code link
image
how can i get it?

Can someone please review my code below. My problem is that regardless of who is calculated as the winner, the computer score is always increased by 1. I didn’t really understand prompt 5 where it asks us to create the upgradeScore function, so I coded it my own way based on what I thought made sense. Everything else seems to work correctly (all three numbers are generating, the correct winner is being calculated, and the round number increases), I just can’t seem to get the score to increase the right way.

on line 20 (which is inside the updateScore function)
your compareGuesses() call has no arguments in the ()
but the compareGuesses function has 3 parameters earlier:

const compareGuesses = (userGuess,computerGuess,targetNumber) => {

However, I think the instructions don’t say anything about using compareGuesses()
[although I might have an older version of the project than the one currently in use]
I think you’re supposed to deal with a string that have a value of 'human' or 'computer' in that function instead.

Step 5

Create an updateScore() function. This function will be used to correctly increase the winner’s score after each round.

This function:

  • Has a single parameter. This parameter will be a string value representing the winner.
  • Increases the score variable ( humanScore or computerScore ) by 1 depending on the winner passed in to updateScore . The string passed in will be either 'human' or 'computer' .
  • Does not need to return any value.

I think the step of using the results of the compareGuesses function to determine who won is handled by the other file, game.js so you don't have to do it yourself.
stuff already in game.js
  // Determine if the human or computer wins:
  const humanIsWinner = compareGuesses(currentHumanGuess, computerGuess, target)
  const winner = humanIsWinner ? 'human' : 'computer'

  // Update the correct score:
  updateScore(winner);

It’s important to stress that we should not edit the game.js file until after we pass this exercise. Follow the instructions as closely as possible. Technically, there is enough information to do this without ever looking at game.js.

Hello,
Here is my very first JavaScript project.
Would appreciate any feedback!
Greetings,
Julia

number-guesser (gitHub)

1 Like

first JS project :slight_smile: i gave the bonus questions a whirl. browser throws the alert when you type a neg number

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

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

// step 4
let compareGuesses = (human, computer, target) => {
    const userG = getAbsoluteDistance(target, human);
    const computerG = getAbsoluteDistance(target, computer);
    if (human < 0 || human > 10) {
      alert(`guess must be between 0 and 9, try again!`)
    }
    return userG <= computerG;  
};

// step 5
const updateScore = winner => {
    if (winner === 'human') {
        humanScore += 1;
    } else if (winner === 'computer') {
        computerScore += 1;
    }
};

// step 6
const advanceRound = () => 
    currentRoundNumber += 1;

// step 8 a & b are 2 nums
const getAbsoluteDistance = (a, b) => {
   return Math.abs(a - b);
};


1 Like

Blockquote Hello There

I have a question. In the function below there’s three parameters: humanGuess, computerGuess, target.
Where do the arguments for these parameters come from?

function generateTarget(){
  return Math.floor(Math.random() * 10);
}

function compareGuesses(humanGuess, computerGuess, target) {
  const computerG = Math.abs(target - computerGuess);
  const userG = Math.abs(target - humanGuess);
  return userG <= computerG;
}

Those are locally declared parameters that get their values from the function call to compareGuesses. They come from other parts of the program and will likely have different names than those of the parameters.

winner = compareGuesses(human, comp, targ)

where human would be the current value in the input field, comp would be the randomly generated value, as would be targ.