# Programme does't work

Hey guys, a newbie here!

i’m trying to create a programme that given a random number should check if one of the two guesses nailed it. In case none of the options are correct the programme should tell which one got closer to the random number.

so far I’ve created the following:

var playerOne = 5;
var playerTwo = 4;
var casual = Math.floor(Math.random() * (100-1) + 1);
var wrongOne = (playerOne != casual);
var wrongTwo = (playerTwo != casual);
var a = (casual - playerOne);
var b = (casual - playerTwo);
var closer0ne = (a < b);
var closerTwo = (a > b);

console.log("Casual number is: " + casual);
console.log("Player one choose the number: " + playerOne);
console.log("Player two choose the number: " + playerTwo);

if (playerOne === casual) {
console.log("Well done player one! you nailed it!");
}
if (playerTwo === casual) {
console.log("Well done player two! you nailed it!");
}
else if (wrongOne) {
if (closer0ne) {
console.log("No one guess it right, but player one is the one got closer!");
}
}
else if (wrongTwo) {
if (closerTwo){
console.log("No one guess it right, but player two is the one got closer!");
}
}

As said i’m learning, I’m ok with the random number, I’m ok in checking if one of the answer is correct but i can’t process the right code to check which numbers is the closer. Any suggestion please?

Thanks a lot!

P.S. I probably should have use less variables and trying to make it shorter but this way of breaking it down I thought it’d have been easier to sorted out.

I see a couple of problems.

First of all, you have a variable called “closer0ne” that uses the number zero instead of the letter “O” - that is really “dangerous” - in the sense that one of your coworkers will slash your tires because they spent 2 hours trying to debug something.

The other problem is that your logic here:

var a = (casual - playerOne);
var b = (casual - playerTwo);

You are finding the distance from the correct answer. But it really doesn’t do that - it finds the difference. The distance is the absolute value of the distance, so I’d want something like:

var a = Math.abs(casual - playerOne);

Next, I think your logic is a little overly complicated. You have a chain of if/else and only one of those can be true. I would make it a complete chain. And if it gets past the first two, you don’t have to check if something is wrong or not - it can’t get that far something wasn’t wrong. I would do something like:

if (playerOne === casual) {
console.log("Well done player one! you nailed it!");
} else if (playerTwo === casual) {
console.log("Well done player two! you nailed it!");
} else if (closerOne) {
console.log("No one guess it right, but player one is the one got closer!");
} else { // only on possibility left
console.log("No one guess it right, but player two is the one got closer!");
}

There are other ways to organize this, but this is keeping with what you were trying to do.

There is the other possibility that isn’t accounted for here - What if both are equally distant? You need to insert a third condition in there for if a === b.

Hi Kevin, thanks again for your help, it’s really appreciated!

First of all, I’m really happy I have neither a car nor a bike! I really don’t know how that “0” end up there…will be more careful from now on.

I couldn’t wrap my head around why:

var a = (casual - playerOne);

was not correct as it was evaluating true or false…and I still struggle to understand why “the distance is the absolute value of the distance” it to be honest…I must reading/studying more about it…
My logic at the beginning was simple as you wrote to be honest (I know it sounds dodgy saying it now with the solution) but because I couldn’t get the right outcome I tried to add more and more, even if often this brings me to forget that as a beginner the exercises I’m going through are basic, therefore simplicity it’s what I should be after…

Thanks for point out the last third condition as well…it reminded me there’s always space to get a code better!

Cheers!

Sorry, I mistyped, I should have said the distance is the absolute value of the difference.

If we both live on the same highway and you love at mile marker 17 and I live at mile marker 12, what is the difference? It depends. If it is from your house, it is 17-12 or 5. If it is from my house it is 12-17 or -5. That negative makes a big difference when you are comparing. If I had a friend at mile marker 11, and I wanted to know who is farther, I’d say that you are at 12-17 or -5 and my friend is at 5-4 or 1, -5 is smaller than 1 so you are closer. That is why you need the absolute values. You have the same problem with your calculation.

Thanks for point out the last third condition as well…it reminded me there’s always space to get a code better!

Yeah, this is what we might call an “edge condition”, that odd ball condition that we didn’t really think about. I suppose another one would be if they both guess the same thing. Yeah, that’s a part of coding, thinking about edge conditions, but even the best coders miss them sometimes.

Hi Kevin, thanks again…it’s clear now, I think.
I was “tricked” by cases that I wrote down at the beginning to do some trials. My programme was working without the “Math.abs” (no random value created but with a fixed var casual) and the “var a” was always greater/lesser (exchanging vars value between them) than “var b” and their absolute value as well, e.g. (I hope it’s clear what I mean):

var playerOne = 78; /* or 56 */
var playerTwo = 56; /* or 78*/
var casual = 100;
var wrongOne = (playerOne != casual);
var wrongTwo = (playerTwo != casual);
var a = (casual - playerOne);
var b = (casual - playerTwo);
var closerOne = (a < b);
var closerTwo = (a > b);

console.log("Casual number is: " + casual);
console.log("Player one choose the number: " + playerOne);
console.log("Player two choose the number: " + playerTwo);

if (playerOne === casual) {
console.log("Well done player one! you nailed it!");
}
else if (playerTwo === casual) {
console.log("Well done player two! you nailed it!");
}
else if (closerOne) {
console.log("No one guess it right, but player one is the one got closer!");
}
else {
console.log("No one guess it right, but player two is the one got closer!");
}

When the casual number was created instead, the absolute value I was not considering made me bump into reality as your “mile markers” example explained now why I should have taken it in consideration!

Thanks again!

Cool, yeah, that’s part of being a developer is trying to think through all the possible cases. But it’s a skill that you develop. And like I said, even then best developers miss them sometimes.