Number Guesser Challenge Project (JavaScript)

I like the way you’ve used the one line method in most of your functions. Your code is clear and well structured.

Hi team,

why is this code not working, thanks!

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

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

const compareGuesses = () => {
const computerGuess = getRandomInt(10)
const diffUser = Math.abs(generateTarget() - userGuess)
const diffComputer = Math.abs(generateTarget() - computerGuess)
if (diffComputer >= diffUser) {
return true
} else if (diffComputer < diffUser){
return false}
}

const updateScore = winner => {
if (winner===‘human’) {humanScore +=1
} else if (
winner===‘computer’
) {computerScore +=1}
}

const advanceRound = () => {return currentRoundNumber +=1}

let humanScore = 0;

let computerScore = 0;

let currentRoundNumber = 1;

// Write your code below:

const generateTarget = () => {

let targetNumber = Math.floor(Math.random() * 9);

return targetNumber;

};

console.log(generateTarget());

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

if (userGuess === computerGuess) {

return true;

} else if (Math.abs(userGuess - secret) > Math.abs(computerGuess - secret)) {

return false;

}else {

return true;

}

if (userGuess > 10) {

 alert('pelease choose from 1 to 10');

}

};

const updateScore = winner => {

if (winner === ‘human’) {

 humanScore ++;

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

computerScore ++;

}

};

const advanceRound = () => {

currentRoundNumber ++;

};

updateScore(‘human’);

console.log(humanScore); // To confirm that this value increased by 1

updateScore(‘computer’);

console.log(computerScore);

i don’t know how to add the alert() functionality and can’t understand what u want from the step 8 where u need me to add a new function called getAbsoluteDistance().

another problem happened is after the first round if the human won the score is human 2 computer 1, and if computer won the score is computer 2 and human 1. why does the first round the computer and the human score increase by one.

I had to use lots of resources, but can someone give me a code review and tell me how to make my code even better!

Ridiculously excited about my first GitHub project :grinning:

I think there’s a problem in the compareGuesses function

I think getRandomInt(10) should be generateTarget()
and
avery generateTarget() afterward should actually be computerGuess

Otherwise you’re generating a new number for the computer for every comparison or calculation;
(but the idea is to generate one number for the computer and do several things with that number).

a distance between numbers is just the absolute value of the differences
(as in how far away something would be on a number line).
[ subtract, then do absolute value; because distances can’t be negative numbers ]

Hello everyone, I wonder if anyone can help me with this problem. I do this exercise on Codecademy learning environment and I want to share my solutions. But there is no share code link on my page. I don’t know if this is a mistake or there is some update about the environment that I don’t know. Thank you in advance. Here is the screenshot of my code on the platform and you cannot see the share code icon.

Hello; I managed to somehow get the Number Guessing Project done and along the lines picked up how to use Git/Github to perform commit and push. Also; downloaded VS Code to write the code for this Project so worth the time spent :slight_smile: So; if someone could please help (thank you in advance)

Pushed my Project folder on to Github as follows:

  1. Not sure if my Code works correctly as I ran the program multiple times and sometimes the result feels like something isn’t quite right??

  2. Also; I can’t seem to find the “share” icon in Codecademy even after I select all and can click “Next” to move on?

Looks like the operator is reversed in line 20 of your script.js file.

Line 20:
if ((Math.abs(targetNum - computerGuess)) > (Math.abs(targetNum - humanGuess)))

is functionally the same as line 17:
if((Math.abs(targetNum - humanGuess)) < (Math.abs(targetNum - computerGuess)))

If you change line 20 to evaluate as “less than”:
if ((Math.abs(targetNum - computerGuess)) < (Math.abs(targetNum - humanGuess)))

that should work.

1 Like

Hi, I have done the numbers game, but I can’t get it to work. I have no idea why.
You can check out the code on GitHub - Tides-group/numbergame: numbergame codecademy. as I am at a loss.

I have matched it against the solution but I still don’t understand why my code is not working.

thank you so much for your reply

rgds

Tim

I just tried to paste the solution in the online terminal, and it also gives errors… So I really am at a loss here :pleading_face:

Here’s my solution to the challenge: Challenge Project: Number Guesser

Here is my code for this project:

Compare your code to mine maybe it can help you figure out what went wrong

let humanScore = 0;

let computerScore = 0;

let currentRoundNumber = 1;

// Write your code below:

function generateTarget() {

// Return a random number 0 - 9

return Math.floor(Math.random() * 9 + 1);

}

// Test Statement

// console.log(generateTarget());

function compareGuesses(userGuess, computerGuess, targetGuess) {

// My first method of logic using the Math.abs()

// if (Math.abs(userGuess - targetGuess) == Math.abs(computerGuess - targetGuess)) {

// return true;

// } else if (Math.abs(userGuess - targetGuess) < Math.abs(computerGuess - targetGuess)) {

// return true;

// } else {

// return false;

// }

if (difference(userGuess, targetGuess) == difference(computerGuess, targetGuess)) {

return true;

} else if ( difference(userGuess, targetGuess) < difference(computerGuess, targetGuess)) {

return true;

} else {

return false;

}

}

// Test Statement

// console.log(compareGuesses(7, 4, 6));

// console.log(compareGuesses(9, 9, 9));

// console.log(compareGuesses(8, 4, 3));

// Using this method now and have a additional commented out if statement as well using Math.abs() directly

function difference(a, b) {

return Math.abs(a - b);

}

const updateScore = winnerIs => {

// Human Case if Statement

if (winnerIs === ‘human’) {

humanScore++;

}

// Computer Case if Statement

if (winnerIs === ‘computer’) {

computerScore++;

}

}

const advanceRound = () => currentRoundNumber++;

1 Like

https://gist.github.com/Passenger89/ae52d59aaf73a1f29b4222cafb2a998f

I found some things in the comments here, but then I realized I read it in Jon Duckett’s JavaScript & jQuery that’s one of the recommended resources. To my understanding window.alert was not used before this exercise.

Hello! Here is my solution

1 Like

Here is my solution any comments are welcome :smiley:

Number Guesser Challenge