Could someone help me to understand what I’m doing wrong in this code:
let generateTarget = Math.floor(Math.random() * 10);
let humanGuess = Math.floor(Math.random() * 10);
let compGuess = Math.floor(Math.random() * 10);
console.log('Target is: ' + generateTarget);
console.log('Human is: ' + humanGuess);
console.log('Computer is: ' + compGuess);
//Determine compDifference
if (compGuess > generateTarget) {
let compDifference = (compGuess - generateTarget);
} else (generateTarget >= compGuess) {
let compDifference = (generateTarget - compGuess);
}
//Determine humanDifference
if (humanGuess > generateTarget) {
let humanDifference = (humanGuess - generateTarget);
} else if (generateTarget >= humanGuess) {
let humanDifference = (generateTarget - humanGuess);
}
//Determine winner
if (compDifference > humanDifference) {
console.log('You won');
} else if (humanDifference > compDifference) {
console.log('You lost');
} else {console.log('It was a tie');
}
I’m sure there are better ways of doing this, but this method is the only one I currently understand, so I’m just trying to figure out why this isn’t working. Thanks