Skip to content

Instantly share code, notes, and snippets.

@codecademydev
Created January 31, 2020 12:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save codecademydev/d456e9f3c87ba9a28d3cfe6074eed1b9 to your computer and use it in GitHub Desktop.
Save codecademydev/d456e9f3c87ba9a28d3cfe6074eed1b9 to your computer and use it in GitHub Desktop.
Codecademy export
let humanScore = 0;
let computerScore = 0;
let currentRoundNumber = 1;
// Write your code below:
//step 3
let generateTarget = () => {
return Math.floor(Math.random() * 9);
};
//step 4
let compareGuesses = (human, computer, target) => {
const userG = Math.abs(target - human);
const computerG = Math.abs(target - computer);
return userG <= computerG;
/*if (userG <= computerG) {
updateScore('human');
} else {
updateScore('computer');
}*/
};
//step 5
let updateScore = winner => {
if (winner === 'human') {
humanScore += 1;
} else if (winner === 'computer') {
computerScore += 1;
}
};
//step 6
function advanceRound() {
currentRoundNumber += 1;
};
@jkalandarov
Copy link

In compareGuess function I see only one combination. Did it work for different situations? :

  1. target >= userGuess && target >= computerGuess //For example, Target: 8, userGuess: 5, computerGuess: 6
  2. target <= userGuess && target <= computerGuess //For example, Target: 1, userGuess: 5, computerGuess: 6
  3. target >= userGuess && target <= computerGuess //For example, Target: 5, userGuess: 2, computerGuess: 6
  4. target <= userGuess && target >= computerGuess //For example, Target: 5, userGuess: 6, computerGuess: 2
    Have you tried against combinations?

@trumanKnight
Copy link

@jkalandarov the only condition that needs to be true for a win is that the human guess is closer to or equal in distance from the target as the computer guess. humanG and computerG are defined as the absolute value from the target guess respectively, so all these cases are handled in the return statement humanG <= computerG. For clearer understanding, maybe the author could have used userD and computerD (D for distance) instead of G (for guess).

All around nice clean code. You used some shortcuts that I over looked when doing my project, and will look to be using them in the future. Thanks for sharing!

@MR10833768
Copy link

Clean Codes are the best ones and its seem like you are on top of the coding short-cuts!.....

@otunbajude
Copy link

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

// Write your code below:

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 )
if (humanDifference < computerDifference) {
return 'Human won!'
} else if (computerDifference < humanDifference) {
return 'Computer won!'
} else {
return 'Human won!'
};
};
const updateScore = winner => {
if(winner === 'human'){
humanScore++;
}
else if (winner === 'computer'){
computerScore++;
}
};
const advanceRound = ()=>{
currentRoundNumber = currentRoundNumber + 1;
};

@irrocau
Copy link

irrocau commented Jul 22, 2021

I don't understand line 8: return Math.floor(Math.random() * 9)
I thought Math.random() returned a number between 0 and 1, 1 exclusive, so if we multiply it by 9, then 9 wouldn't be possible either? And then we use Math.floor which rounds to the nearest lower integer, so the result should be [0; 8]. Please, can someone explain what I'm missing?

@jkalandarov
Copy link

I don't understand line 8: return Math.floor(Math.random() * 9)
I thought Math.round() returned a number between 0 and 1, 1 exclussive, so if we multiply it by 9, then 9 wouldn't be possible either? And then we use Math.floor which rounds to the nearest lower integer, so the result should be [0; 8]. Please, can someone explain what I'm missing?

Math.round rounds floating numbers to integers; e.g. 1.75 -> 2 or 1.25 -> 1. It does not generate random number.

@irrocau
Copy link

irrocau commented Jul 22, 2021

I don't understand line 8: return Math.floor(Math.random() * 9)
I thought Math.round() returned a number between 0 and 1, 1 exclussive, so if we multiply it by 9, then 9 wouldn't be possible either? And then we use Math.floor which rounds to the nearest lower integer, so the result should be [0; 8]. Please, can someone explain what I'm missing?

Math.round rounds floating numbers to integers; e.g. 1.75 -> 2 or 1.25 -> 1. It does not generate random number.

Sorry, I meant to write "Math.random", not Math.round! Will edit now

@jkalandarov
Copy link

I don't understand line 8: return Math.floor(Math.random() * 9)
I thought Math.round() returned a number between 0 and 1, 1 exclussive, so if we multiply it by 9, then 9 wouldn't be possible either? And then we use Math.floor which rounds to the nearest lower integer, so the result should be [0; 8]. Please, can someone explain what I'm missing?

Math.round rounds floating numbers to integers; e.g. 1.75 -> 2 or 1.25 -> 1. It does not generate random number.

Sorry, I meant to write "Math.random", not Math.round! Will edit now

Math.random() * x generates floating numbers between 0 and x, not reaching x. You have to use Math.floor(Math.random() * x) to round down to the nearest integer, otherwise the returned value won't be an integer.

@OmarCab97
Copy link

Line 15: return userG <= computerG;

why it should return User Guess is minor or equal to ComputerG?

@lgomez92
Copy link

Line 15: return userG <= computerG;

why it should return User Guess is minor or equal to ComputerG?

userG <= computerG, will return a boolean value, if userG is less than or equal to computerG the function will return true, otherwise it will return false

@koekjesdoos
Copy link

Hi

I think indeed it should be Math.floor(Math.random()*10)

kind regards

@mrstosh
Copy link

mrstosh commented Apr 21, 2022

/!\ I don't know how my code doesn't let my humanScore +1 when human is winning, I can't find more mistake in my code and thats driving me mad please help ! /!\

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

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

function compareGuesses(user, computer, target) {
const userGuess = Math.abs(target - user);
const computerGuess = Math.abs(target - computer);
return userGuess <= computerGuess;
/* if (userGuess <= computerGuess) {
return updateScore("user");
} else if (computerGuess <= userGuess) {
return updateScore("computer");
}
*/
}

function updateScore(winner) {
if ("winner" === "user") {
humanScore++;
} else if ("winner" === "computer") {
computerScore++;
}
}

function advanceRound() {
return currentRoundNumber++;
}

@koekjesdoos
Copy link

koekjesdoos commented Apr 22, 2022 via email

@DannyRuda
Copy link

/!\ I don't know how my code doesn't let my humanScore +1 when human is winning, I can't find more mistake in my code and thats driving me mad please help ! /!\

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

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

function compareGuesses(user, computer, target) { const userGuess = Math.abs(target - user); const computerGuess = Math.abs(target - computer); return userGuess <= computerGuess; /* if (userGuess <= computerGuess) { return updateScore("user"); } else if (computerGuess <= userGuess) { return updateScore("computer"); } */ }

function updateScore(winner) { if ("winner" === "user") { humanScore++; } else if ("winner" === "computer") { computerScore++; } }

function advanceRound() { return currentRoundNumber++; }

@mrstosh Your mistake is inside the code block of your function "updateScore". In the condition, that the if statement checks, you wrote
" "winner" === "user" " but because you used quotation marks around "winner", the programm treats that as a string and not as the parameter of the function. So, you just have to leave the quotation around "winner" in both if statement conditions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment