Number Guesser Challenge Project (JavaScript)

Not sure what I am doing wrong. Rounds will not advance and scores will not add up.

let humanScore = 0; let computerScore = 0; let currentRoundNumber = 1; // Write your code below: const generateTarget = () => { return Math.floor(Math.random() * 10) } const compareGuesses = (humanNum, computerNum, targetGuess) => { const humanGuess = Math.abs(targetGuess - humanNum); const compGuess = Math.abs(targetGuess - computerNum); return humanGuess <= computerGuess } const updateScore = winner => { if (winner === 'human') { humanScore++ } else if (winner === 'computer') { computerScore++ } }; const advanceRound = () => currentRoundNumber++;

line 13 in the Codebyte has a typo:
compGuess
should probably be
computerGuess

(Its gray in the codebyte so that variable is not used for anything later [with that name].)

1 Like

Hello guy this is my code check this out!! it work normally only one thing I miss is step 8 part one if anyone know how make it work please let me know

this is my Github check it out : (java-number-guesser/script.js at master · kombattle/java-number-guesser · GitHub)

I used the following for step 8 part one:
(spoiler below)

Replacing some of the code for step 4:

const getAbsoluteDistance = (a, b) => Math.abs(b - a);

in your version, you could use this instead:

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

the new version would be

const humanDif = getAbsoluteDistance(targetGuess, humanGuess );

similarly for

const computerDif

Be prepared, @janbazant1107978602 to explain at length for the learners coming up this path. It’s a long road when we post solutions, however couched. In other words, a commitment. We’re counting on that, else later users will carry the burden. “How does this work?” “Can someone explain…” We get it all the time. Are you in for the duration?

1 Like

Please why is my number guesser project not working. I am sure the code is correct but it just wouldnt work. The page is instead showing up as a static page.

My code for Number-Guesser on GitHub. I would like the opinion on the repeatRound() function in case the user choise is negative number or over 9.

Not getting a Winner, prompt for next round and score does not change.

let humanScore = 0; let computerScore = 0; let currentRoundNumber = 1; // Write your code below: function generateTarget(){ return Math.floor(Math.random()*10); } function compareGuesses(user, cpu, target){ let human = math.abs(user-target); let com = math.abs(cpu-target); if (user>9 || user<0) alert('Enter number between 0 and 9'); if (human<=com) return true; else return false; }; function updateScore(winner){ if (winner==='human') humanScore++; else if (winner==='computer') computerScore++; }; function advancedRound(){ currentRoundNumber++ };

typo here:
math.abs should be Math.abs

1 Like

thanks! having trouble with the next round button. its not starting the next round.

typo here:
function advancedRound()
should be
function advanceRound()

1 Like

wow not sure if i should be happy it wasnt a bigger issue or said i missed something so simple lol. either way thanks for the assist.

Here is what I came up with:

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

// returns the distance between 2 numbers
getAbsoluteDistance = (p1, p2) => {
    return Math.abs(p1 - p2);
};

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

// returns true if the human player is closer or equal to the computer
compareGuesses = (human, com, target) => {
    if (human < 0 || human > 9) {
        // clamps the guess between 0 and 9
        human = Math.min(Math.max(human, 0), 9);

        alert(
            `You can only enter a value between 0 and 9!\n\nChanging number to ${human}`
        );
    }

    humanDistance = getAbsoluteDistance(target, human);
    comDistance = getAbsoluteDistance(target, com);

    return humanDistance <= comDistance;
};

updateScore = (player) => {
    player == "human" ? humanScore++ : computerScore++;
};

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

If you have any feedback feel free to reply!

Hello! here is my submission :slight_smile: , I attempted to extend the project with the final challenge but ultimately fell short.

Hi All, I’m having some trouble with my getAbsoluteDistance() portion of the problem:

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.

I find that sometimes the game works, and other times the winner does not make sense (see screenshot of computer difference being 4 and mine being 6, yet I was marked as the winner. My code is also pasted below. Help you help me trouble shoot?

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

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

const getAbsoluteDistance = (humanGuess, computerGuess) => {
  const humanDifference = Math.abs(humanGuess - targetGuess)
  const computerDifference = Math.abs(computerGuess - targetGuess)
}

const compareGuesses = (humanDifference, computerDifference) => {
  return humanDifference <= computerDifference
}

const updateScore = winner => {
  if (winner === 'human') {
    humanScore++;
  } else if (winner === 'computer') {
    computerScore++;
  }
}
const advanceRound = () => currentRoundNumber++;

I think your getAbsoluteDistance function is missing parameter targetGuess,
meaning its should start
const getAbsoluteDistance = (humanGuess, computerGuess, targetGuess) =>

Also, I think you need to redo the compareGuesses function:
the parameters(inputs) should be the humanGuess, ComputerGuess, and the target.

humanDifference and computerDifference are variables that need to be computed inside that function.
(They are the absolute differences from the target.)

The setup on GitHub link - is 404
What do you recommend I read in order to connect to gitHub from vscode ?