Your function is an acceptable way of accomplishing the task. It produces the expected outcome perfectly well. It could be refactored to be significantly more concise if you have the desire to do so. Consider this example:
const compareVals = (val1, val2) => val1 <= val2 //concise body arrow function
//could also be written:
/*
const compareVals = (val1, val2) => {
return val1 <= val2
}
*/
console.log(compareVals(5, 6)) //true
console.log(compareVals(5, 5)) //true
console.log(compareVals(5, 4)) //false
The expression val1 <= val2 is evaluated, and that result is returned. To apply this to your code, keep in mind that val1 and val2 could also be expressions. You are currently comparing expressions like this:
So… I reviewed the initial post on how the function could possibly look, but to be honest I completely got stuck on how to build the “compareGuesses” function. So, now I am feeling pretty dumb at this point. How do you even come to the conclusion that the function should be structured that way (I guess I do not know how to problem solve properly)?
One suggestion in the narrative is to abstract away the absolute difference to be a helper function.
const absDiff = (a, b) => Math.abs(a - b);
We’ll come back to this once we deal with the question, if the user wins on a tie, then do we need that conditional? Once we know the computer does not win we can default to the user.
Hello All!
I took a little break from learning (which was bad on my part); I was just getting really frustrated and burnt out from the learning and projects. BUT I have returned back to my lesson! Trying to finish up this project in particular now, and after spending two days with it I feel like I have made some progress! I wanted to get another pair of eyes on my code. I am not getting the results I require to complete the project, so any suggestions would be greatly appreciated.
let humanScore = 0;
let computerScore = 0;
let currentRoundNumber = 1;
// Write your code below:
// Random number generator: 0 - 9 / function one
const generateTarget = () => {
let randomNum = Math.floor(Math.random() * 10);
return randomNum;
};
const compareGuesses = (user, computer, target) => {
if (Math.abs(user - target) <= Math.abs(computer - target)) {
return 'User Wins!';
} else if (Math.abs(user - target) < Math.abs(computer - target)) {
return 'User Wins!';
} else if (Math.abs(computer - target) < Math.abs(user - target)) {
return 'Computer Wins!';
} else {
return 'Error!'
}
};
// Increases the winner's score after each round / function three
const updateScore = () => {
humanScore = ++0;
computerScore = ++0;
return `Score: User has ${humanScore} points. Computer has ${computerScore} points.`;
};
// Updates the round number after each round / function four
const advanceRound = () => {
currentRoundNumber = ++1;
return `Round: ${currentRoundNumber}`;
};
console.log(advanceRound());
console.log(compareGuesses(5, generateTarget(), generateTarget()));
console.log(updateScore());```
In my terminal, I get this error message:
SyntaxError: Invalid left-hand side expression in prefix operation
at wrapSafe (internal/modules/cjs/loader.js:1053:16)
at Module._compile (internal/modules/cjs/loader.js:1101:27)
at Object.Module._extensions…js (internal/modules/cjs/loader.js:1157:10)
at Module.load (internal/modules/cjs/loader.js:985:32)
at Function.Module._load (internal/modules/cjs/loader.js:878:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
at internal/main/run_main_module.js:17:47
Hello mtf
could you please help me as i am not clear about > < ?
if (absDiff(cpu, targetNum) < absDiff(user, targetNum)) - is it not > in order to put ‘computer wins’ as it should be the nearest number to targetNum win ? i put numbers in this consideration, lets say targetNum is 5, cpu is 4 and user is 3. So absDiff (targetNum, cpu ) is 1 and absDiff (targetNum,user) is 2. 2 is near than 1 to targetNum so if absDiff (targetNum, cpu ) is < absDiff (targetNum,user) , should user be winning ?
My code is below but i am still not correct but i don’t know what to do anymore and i am stacked
.
I want to use ‘Booleans’ ‘true’ ‘false’ , just to try alternative but i am not sure i can use Booleans in this case ?
it kept on showing red colour and error so i cannot continue.
It already is that in the body of the function. You generate a random number just fine, but the number isn’t returned.
//either:
function generateTarget() {
let round = Math.floor(Math.random() * 9);
return round;
}
//or simply:
function generateTarget() {
return Math.floor(Math.random() * 9);
}
//or if you're into concise body arrow functions
const generateTarget = () => Math.floor(Math.random() * 9);
Also check your spelling. The function name has to be generateTarget. The code that calls your function is in another file called game.js. If the name doesn’t match, the game won’t work.
sorry mtf, i don’t get it . Should i simply write like below as i don’t know what to do with Booleans anymore:)
if (userG<computerG) { return true: }
if (userG=computerG) (return true;}
else { return false;}
Thanks a lot mtf, now I totally understood.I prefer to use Booleans as it is shorter. I am slowly getting all these conditional things by practising like this.