I am stuck in task number 4. The task says following:
Create a compareGuesses() function. This function will be called each round to determine which guess is closest to the target number.
Has three parameters representing the user (human) guess, a computer guess, and the secret target number to be guessed.
Determines which player (human or computer) wins based on which guess is closest to the target. If both players are tied, the human user should win.
Return true if the human player wins, and false if the computer player wins.
I have already created below function as per the above mentioned task and when I wanted to test it out, I called it with example values and it looks like my function is wrong, I guess, I made an error in the logic, can someone kindly assist me please? So I can get the correct code and move on to the next task in the project.
If you have a result in the form of a boolean and you want a boolean, then rather than testing whether the boolean is true and then returning true / otherwise false… return the boolean that you already had.
There are two cases, player1 wins, player2 wins. You have three branches in your if-statement.
If you’re testing which is closer then that would be talking about two distances, but you’re doing the whole abs(a - b) thing four times and they’re all different. If there are two things that you should be considering but you have four, then what are the other two?
You’re missing two semi-colons. It’s easier to skip all of them than to remember to put them in each place they belong.
If they don’t do anything then you may as well not write them out.