Here my compareGuesses function always return false what’s the problem in my logic i don’t understand.
let humanScore = 0;
let computerScore = 0;
let currentRoundNumber = 1;
let userGuess = 5;
let computerGuess = Math.floor(Math.random() * 9);
// Write your code below:
const generateTarget = () => {
return Math.floor(Math.random() * 9);
}
let target = generateTarget();
const compareGuesses = (userGuess, computerGuess, target) => {
//let valueH = Math.abs(userGuess - target);
//let valueC = Math.abs(computerGuess - target);
if((Math.abs(userGuess - target)) <= (Math.abs(computerGuess - target))) {
return true;
} else {
return false;
}
}
const winner = compareGuesses(userGuess, computerGuess, target) === 'true' ? 'human' : 'computer';
const updateScore = (winner) => {
if(winner === 'human') {
humanScore += 1;
} else if(winner === 'computer') {
computerScore += 1;
} else {
console.log('Error: something might be wrong');
}
}
const advanceRound = () => {
currentRoundNumber += 1;
}
console.log(computerGuess);
console.log(userGuess);
console.log(target);
console.log(compareGuesses());
yizuhi
September 2, 2020, 1:23pm
2
I think this might be the problem.
Your compareGuesses()
function is set to return either true
or false
. Later on, you assign the variable winner
to the result of calling your compareGuesses()
function, which means winner
will now hold a Boolean value (either true
or false
).
But then inside your updateScore()
function, you’re trying to check if winner
has the value ‘human’ or ‘computer assigned to it. That’s not gonna work, since the winner
value will NEVER hold either of those values. It will always hold either true
or false
.
Hope this helped you
Is the problem with compareGuesses() If condition logic…
yizuhi
September 3, 2020, 1:13pm
4
First of all, I’m truly sorry for what I said before, because I just realized that wasn’t the problem
What’s the output when you run your code?
_max
September 3, 2020, 1:22pm
5
look at your compareGuesses
if () {
compareGuesses() output is always show else part output false.
I look it many times but i don’t find the problem pls help me
_max
September 4, 2020, 1:14pm
8
Hello, I’ve recently begun learning JavaScript. I try to understand the theory and mentality of everything before moving over. I realize I could just play around and figure out the correct code to make this project function, however then I’d be at a loss. Therefore I humbly ask that someone who knows JavaScript and is familiar with the number guesser game review my code and tell me where I have gone wrong;
Number Guesser Project Link
let humanScore = 0;
let computerScore = 0;
let currentRoundN…
see if this helps, I don’t have notes for that project, or pro access, so a bit blind here.