 Number Guesser Challenge Project (JavaScript)

Here is my solution any comments are welcome Number Guesser Challenge

Hello If you want to check my work it’s here My number guesser

Thanks to @rodlestermoreno37925 who gave me inspiration for the form and content of my GITHUB repo CodeCademy

1 Like

Hi, I was wondering if somebody could tell me why my code isn’t working with the game? I checked the sample code and I don’t think any of my functions are wrong, but when I try out the number guesser in the browser, it doesn’t keep track of the score, and the round number doesn’t change. I’m not really sure why this is happening.

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

function generateTarget() {
return Math.floor(Math.random() * 10);
}
let targetNumber = generateTarget();
function compareGuesses(humanGuess, computerGuess, targetNumber) {
let humanDiff = Math.abs(humanGuess - targetNumber);
let computerDiff = Math.abs(computerGuess - targetNumber);
if (humanDiff === computerDiff || humanDiff < computerDiff) {
return true;
} else {
return false;
}
}
if (winner === ‘human’) {
humanScore + 1;
} else if (winner === ‘computer’) {
computerScore + 1;
}
}
currentRoundNumber + 1;
}

humanScore + 1;
should be
humanScore = humanScore + 1;

otherwise the value of that variable won’t change when you call the function.

And similarly for computerScore and for currentRoundNumber.

Hi ,

My script.js code below , and full project here

let humanScore = 0;

let computerScore = 0;

let currentRoundNumber = 1;

//generate the new secret target number.

const generateTarget = () => {

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

return randomNum;

}

//calculate absolute distance from target

const getAbsoluteDistance = (start, target) => {

return Math.abs(start - target);

}

//determine which guess is closest to the target number.

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

if ( humanGuess < 0 || humanGuess > 9) {

}

return getAbsoluteDistance(humanGuess, target) <= getAbsoluteDistance(computerGuess, target) ? true : false;

}

//increase the winner’s score after each round.

const updateScore = winner => {

winner == 'human' ? humanScore++ : computerScore++;

}

//update the round number after each round

const advanceRound = () => {

currentRoundNumber++;

}

Hey!

Here is my solution. Please let me know if there is anything I should add to make my code better and more effective.

Thank you

Hello all!

Here’s my code! Please check it out and let me know where I can improve!

let humanScore = 0;

let computerScore = 0;

let currentRoundNumber = 1;

//1stfunction

function generateTarget() {

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

}

// 2nd function

function compareGuesses(userGuess, compGuess, targetNumber) {

generateTarget();

const userDiff = Math.abs(targetNumber - userGuess);

const compDiff = Math.abs(targetNumber - compGuess);

if (userDiff <= compDiff) {

return true;

} else {

return false;

}

} console.log(compareGuesses(8,2,5))

// 3rdfunction

switch (winner) {

case 'human':

humanScore += 1;

break;

default:

computerScore += 1;

}

}

// 4th function

const advanceRound = () => {

currentRoundNumber += 1;

}

maybe its this:

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

I think generateTarget should be generateTarget()
and similarly for some of the other functions.

Hi fellow students!!

I gladly share you my solution:
https://gist.github.com/f7f969a218e5d5f129a8c2b51381eb20.git

Feel free to comment!

1 Like

Hello all! Here’s my attempt. Let me know if there’s anything I can refactor/do differently. Thanks!

Hey guys,

I just wanted to share my project with you all.

Have a nice one!

Just sharing my version of the game code!

*Hello, I am wondering if anyone can help me refactor my code. I want to see if there is something I could have done that would make my code more readable or efficient. Thank you!

Hi not sure I can help you with feedback on the cleaner code, in fact, I was quite impressed with yours and it is definitely cleaner and clearer than mine. However, I did take a quick glance and saw that when you generated a random target number, you used you Multiplied it by 9 like this:
const generateTarget = () => Math.floor(Math.random() * 9);

Since you are rounding down using the Math.floor method this means that the computer will be selecting between 0-8

The solution to this would be to multiply by 10 like this:

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

1 Like

You are absolutely correct! Thank you so much!

1 Like

This is my first shared code. Hope i did not make any rookie mistakes. I know it works by the way.

Hello!

so I finished working through the original project and was working on the extension. my previous code executed fine. after after attempting to add the absolute distance function lines 18-26 no longer execute though nothing has changed in those respective functions.

Hi I’ve just started this course and here is my code for the Number Guesser. It seems to work fine but would like feedback on how to improve my code. The code has all documentation for all functions to help explain the code. If it helps anyone with the project feel free to read the code or any help on improvements will also be appreciated.
Thanks
git hub - guesser game

My apologies I have copied the wrong link in the reply. That is not my code I have updated the link to my code. Here it is. Sorry for the mixup.

Here’s my solution for the project: Codecademy export · GitHub