-
-
Save codecademydev/d456e9f3c87ba9a28d3cfe6074eed1b9 to your computer and use it in GitHub Desktop.
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 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!
Clean Codes are the best ones and its seem like you are on top of the coding short-cuts!.....
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;
};
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?
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.
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
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.
Line 15: return userG <= computerG;
why it should return User Guess is minor or equal to ComputerG?
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
Hi
I think indeed it should be Math.floor(Math.random()*10)
kind regards
/!\ 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++;
}
/!\ 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.
In compareGuess function I see only one combination. Did it work for different situations? :
Have you tried against combinations?