Number Guesser Challenge Project (JavaScript)

This was the first JS project I managed to do without checking hints or watching the tutorial and it worked! So please be kind :face_in_clouds:

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

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

const compareGuesses = (user, computer, secret) => {
  let generateTarget = secret;
  if (user === secret) {
    return true;
  } else if (computer === user) {
    return true;
  } else if (computer === secret) {
    return false;
  } else if (user < computer && computer < secret) {
    return false;
  } else if (computer < user && user < secret) {
    return true;
  } else if (user > computer && computer > secret) {
    return false;
  } else if (computer > user && user > secret) {
    return true;
  } else return true;
};

const updateScore = () => {
  let winner = ('human' || 'computer');
  if (winner === 'human') {
    humanScore + 1;
  } else if (winner === 'computer') {
    computerScore + 1;
  }
};

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

Hi there! it’s my GitHub and the project “Number Guesser”!

Hello everyone, I have just completed the Guess the Numbers Challenge project (JavaScript) and I would like to share the code.
The number-guessing challenge project (JavaScript) has been completed and the code is shared.
Guess the Numbers Challenge project (JavaScript)

Hi All,

Here is my code. I have been exposed to JS back in college so I did use some techniques that some may not have been exposed to as of yet. Hopefully it is still legible to all.

let humanScore = 0; let computerScore = 0; let currentRoundNumber = 1; // Generate the target number const generateTarget = () => { return Math.floor(Math.random() * 10); }; // Determine the winner of the round const compareGuesses = (humanGuess, computerGuess, targetNumber) => { const humanPoints = Math.abs(humanGuess - targetNumber); const computerPoints = Math.abs(computerGuess - targetNumber); return humanPoints <= computerPoints ? true : false; }; // update the score based on the winner const updateScore = (winner) => { if (winner === "human") humanScore++; else computerScore++; }; // advance the round const advanceRound = () => { currentRoundNumber++; }; // simple test let testTarget = generateTarget(); console.log(`Human #: 8 Computer #: 3 Target #: ${testTarget} Human Won?: ${compareGuesses(8, 3, testTarget)}`);

Josh

I think you did great for your first time! I have two suggestions.

1.) You can use Math.abs to get the absolute value of the range of the user and computer guesses. This allows you to then use a single if…else to compare by checking if Math.abs(user - secret) <= Math.abs(computer - secret). If it is, return true, if not, return false. Too many if statements can cause you a great deal of mental frustration later when you revisit the code and have to try to remember what your if statements are accomplishing. :weary:

2.) In your update, winner should be a parameter of the function like this: const updateScore = (winner) => { … }. This will allow you to remove the line let winner = (‘human’ || ‘computer’);.

You did great and remember that there are many ways to solve the problem. Once you find a solution, go back and try to refactor to improve your functions without changing functionality. There are a few more optimizations that your could do to your code. As you learn more, try to go back and see if you can refactor this code for better optimization! You got this! :+1: :+1:

You need to put winner in brackets.

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

Documentation: Arrow function expressions - JavaScript | MDN

The parentheses can only be omitted if the function has a single simple parameter. If it has multiple parameters, no parameters, or default, destructured, or rest parameters, the parentheses around the parameter list are required.

If there is only one simple parameter in an arrow function, then the parentheses are optional.

const greeting_1 = (name) => {
    console.log(`Hello ${name}!`);
}

const greeting_2 = name => {
    console.log(`Hello ${name}!`);
}

greeting_1("Jack"); // "Hello Jack!"
greeting_2("Jill"); // "Hello Jill!"
1 Like

Ah you’re right! Just saw, you already solved his problem too :sweat_smile:

Hello There!
Check this out.

Repo

Code:
https://garrett04.github.io/CodecademyProjects/Building%20Interactive%20Websites/Number%20Guesser/index.html

Hi :v:

Here’s my solution to this project:

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

// Write your code below:

//Create a new random secret target number at the start of each round.
function generateTarget() {
    return Math.floor(Math.random() * 9);
}

//Determines which player wins based on which guess is closest to the target.
//If both guesses tie, human should win.
function compareGuesses(human, computer, target){
    human < 0 || human > 8 > alert('The number you\'ve provided is out of range. Please enter a number between 0 and 9.');
    return Math.abs(human - target) <= Math.abs(computer-target) ? true : false;
}

//Update the score for the winner by 1.  Accepts 'human' or 'computer'.
function updateScore(winner) {
    winner == "human" ? humanScore += 1 : computerScore += 1;
}

//Update the round number after each round.
function advanceRound() {
    currentRoundNumber += 1;
}

Hi! Here’s my solution :cowboy_hat_face:

1 Like

Hi,

This is my code below but the next round button does not go to next round. I think everything works but the button and I am not sure why?

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

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

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

return humanDifference <= computerDifference;
}

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

const advancedRound = () => currentRoundNumber += 1;

Is it supposed to be advancedRound or advanceRound ?

1 Like

can some one have a look and tell me if I have done this well :
let humanScore = 0;
let computerScore = 0;
let currentRoundNumber = 1;

// Write your code below:
const generateTarget = () => {
const randomNum = Math.floor(Math.random() * 10) + 1;
return randomNum;
};

const compareGuess = (humanGuess, compGuess, targetNum) => {
const humanDiff = Math.abs(targetNum - humanGuess);
const compDiff = Math.abs(targetNum - compGuess);
if (humanDiff < compGuess) {
return “You Win!”;
} else if (humanDiff > compDiff) {
return “Computer Wins!”;
} else {
return “Tie! Play Again!”;
}
};

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

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

const targetNum = generateTarget();
const humanGuess = 5;
const compGuess = 6;
const humanWins = compareGuess(humanGuess, compGuess, targetNum);

updateScore(humanWins);
advanceRound();

Hey folks, here is my approach to this challenge project :slight_smile:

Codecademy export (github.com)

Hi guys, here is my version to this challenge :
https://bumbitzu.github.io/CodeCademyProject-number-guesser-starting/
And the repository:

not really, you have to make some changes

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

// Write your code below:
const generateTarget = () =>
{
  const randomNum = Math.floor(Math.random() * 10) + 1;
  return randomNum;
};

const compareGuess = (humanGuess, compGuess, targetNum) =>
{
  const huamanDiff = Math.abs(targetNum - humanGuess);
  const compDiff = Math.abs(targetNum - compGuess);
  return humanDiff <= compDiff; //just need to return true or false
};

const updateScore = (winner) =>
{
  if (winner === 'human') 
  {
    humanScore++; //add one 
  }
  else if (winner === 'computer')
  {
    computerScore++; //add one
  }
};

const advanceRound = () =>
{
  currentRoundNumber++; //add one
};
1 Like

the last line is wrong:

const advanceRound :white_check_mark:
const advancedRound :x:

1 Like

hello everyone. This is my code. Do you have any suggestion to improve it?

Hi All,

I am having some trouble with this one. Is anyone able to take a gander at my code and let me know what they thing. It does not appear to be working at the moment.

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

// Write your code below:

const generateTarget = () => {
returnmath.floor(math.random()*10);
};

const compareGuess = (humanGuess, computerGuess, target) => {
let humanDistance = Math.abs(humanGuess - target);
let computerDistance= Math.abs(computerGuess - target);
if (humanDistance <= computerDistance){
return true;
} else {
return false;
};
};

const updateScore = () => {
if (compareGuess() === true){
humanScore++;
} else {
computerScore++;
};
};

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