Number Guesser Challenge Project (JavaScript)

Dude that’s awesome. Soon as I get home I’m adding that to my version and checking it out. But that doesn’t solve the subtract button not being disabled in the next round even if the number is 0?

here is my solution with step 8.

My Solution:

To answer your question about the alert, I found the solution somewhere further up in this thread. It has to go in game.js, starting on line 20:

const currentHumanGuess = humanGuessInput.value;
  
  if (currentHumanGuess < 0 || currentHumanGuess > 9) {
    alert('Please enter a value between 0 and 9.');
  };

However, after the alert, the input still gets passed through. Not great.

1 Like

The thing to do might be to add a return statement after the alert.

return false;
2 Likes

What’s wrong with this code???

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

// Write your code below:

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

const getAbsoluteDistance = (targertNumber , playerOrComputerGuess) =>
{
return
Math.abs(targetNumber - playerOrComputerGuess);

}
const compareGuesses = (humanGuess, computerGuess, targetNumber) =>
{
const humanGuessDifference = getAbsoluteDistance(targetNumber, humanGuess); // 7

const computerGuessDifference = getAbsoluteDistance(targertNumber, computerGuess); // 4
/*humanGuessDifference = Math.abs(targetNumber - humanGuess);

computerGuessDifference = Math.abs(targetNumber - computerGuess);*/

if (computerGuessDifference > humanGuessDifference)
{
  return true;
}
else if ( computerGuessDifference < humanGuessDifference)
{
  return false;
}
else
{
  return true;
}

}

const updateScore = string =>
{
if (string === “human”)
{
humanScore++;
}
else if (string === “computer”)
{
computerScore++;
}
}

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

updateScore(‘human’);
console.log(humanScore); // To confirm that this value increased by 1
updateScore(‘computer’);
console.log(computerScore); // To confirm that this value increased by 1

That’s awesome, thank you! I guess I didn’t understand it because I didn’t know there was an alert() function in javascript. I thought I was supposed to create one.

Hi Sir, could you please have a look at my code, it is not working properly!


let humanScore = 0;

let computerScore = 0;

let currentRoundNumber = 1;

// Write your code below:

const generateTarget = () => {

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

};

const compareGuesses = (humanG, computerG, secretTarget) => {

    secretTarget = generateTarget;

    computerG = Math.floor(Math.random() * 10);

    distanceComputer = Math.abs(computerG, secretTarget);

    distanceHuman = Math.abs(humanG, secretTarget);

    if(distanceHuman <= distanceComputer) {

        return true;

    } else {

        return false;

    }

};

const updateScore = (winner) => {

    if(winner === 'human') {

        humanScore++;

    } else if(winner === 'computer') {

        computerScore++;

    };

};

const advanceRound = () => {

    currentRoundNumber++;

};

that is a vague description, what isn’t working?

The problem might be here:

    secretTarget = generateTarget;

you are merely referencing the generateTarget function, you never call/invoke this function

I deleted that line but I have a question here:
1/ if that line is not necessary then, where do I get the secretTarget from?
2/ after I deleted that line, its still not working, I mean, the winner is always random.
3/ I saw a few solution but without the line:
computerG = Math.floor(Math.random() * 10);

then, where do we get the computer Guess from?

Thank you sir for you time!


I always got random winner, not depending on the absolute distance between the numbers. Could you please help me?

And why did you do this?

I didn’t say the line wasn’t necessary, I merely pointed out that you made a mistake on that line

Hello guys! This is my project! What do you think? Is it good? I tested it and it worked! I had some complications with the compareGuesses function, but the rest I was able to do all by myself! Hope you guys can rate it for me and tell me if I am good or not please? Do I have the skills to make it in this domain? Thank you!

// These global variables will be used throughout the code and within their functions

let humanScore = 0;

let computerScore = 0;

let currentRoundNumber = 1;

// This function will be called at the start of each new round in order to generate the new secret target number

generateTarget = () => {

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

}

// This function determines which player (human or computer) wins based on which guess is closest to the target. If both players are tied, the human user should win

const compareGuesses = (humanGuess, computerGuess, targetNumberGuess) => {

if (Math.abs(humanGuess - targetNumberGuess) < Math.abs(computerGuess - targetNumberGuess) || Math.abs(humanGuess - targetNumberGuess) === Math.abs(computerGuess - targetNumberGuess)) {

return true;

} else if (Math.abs(computerGuess - targetNumberGuess) < Math.abs(humanGuess - targetNumberGuess)) {

return false;

}

}

// 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’

const updateScore = winner => {

if (winner === ‘human’) {

humanScore = humanScore + 1;

} else if (winner === ‘computer’) {

computerScore = computerScore + 1;

}

}

// This function will be used to update the round number after each round

const advanceRound = () => {

currentRoundNumber = currentRoundNumber + 1;

}

Here is my github for Number-Guesser-Challenger-Version

i understand nothing about it… :exploding_head:

first… my environment have not a share code link…
image

second… i never knew if my code were right…

third… i´m goin in my firts study month in full stack engineer path at 3rth module… and just now i’m feeling frustrade… is this normal? :woozy_face: :frowning_face: :sob:

1 Like

Thank you, I had a similar problem. I missed to return the value :wink:

There is a bug in game.js on the CA side.

assuming all these bugs are by design?

Looks like the wrong operators and a value was set incorrect.

Also, had no idea you could open different files. Does it mention that in the instructions?

You could take closer look at:

humanGuessInput.addEventListener('input', function(e) { ... });

in the game.js file. It may present something useful.