So, I'm picking up JavaScript since I just seems to enjoy it. I bought an online course, and now facing a challenge in my code. It's a pretty simple one, so hopefully one of the experts out there can help the poor fella (me).
I have the following code, which to my knowledge, should be proper:
var johnAge = 25;
var markAge = 30;
var steveAge = 40;
var johnHeight = 170;
var markHeight = 175;
var steveHeight = 150;
var John, Mark, Steve;
John = johnHeight + 5 * johnAge;
Mark = markHeight + 5 * markAge;
Steve = steveHeight + 5 * steveAge;
console.log('John: ' + John);
console.log('Mark: ' + Mark);
console.log('Steve: ' + Steve);
if (John > Mark && Steve) {
console.log('John wins!');
} else if (Mark > Steve && John) {
console.log('Mark wins!');
} else if (Steve > John && Mark) {
console.log('Steve wins!');
} else {
console.log("it's a draw.");
}
Here is my problem:
John: 295
Mark: 325
Steve: 350
Steve wins!
Now, this game is about having the one with the highest points to win the game. Steve is obviously the winner based on his score.
The problem is with this part right here:
} else if (Mark > Steve && John) {
console.log('Mark wins!');
} else if (Steve > John && Mark) {
console.log('Steve wins!');
It will show that 'Mark' won the game if and when i make the following changes:
} else if (Mark > John && Steve) {
console.log('Mark wins!');
}
I simply just switched locations between 'John' and 'Steve'. If John comes first, it shows that 'Mark won the game', and if Steve comes firs, it moves on and executes the next 'else if' which is 'Steve won the game'.
What I don't understand is why changing positions between my variables causing such huge difference even though I'm using the logical and &&.
This problem doesn't seem to exist when I use binary and &. Though, as far as I've read about this, binary and isn't preferable in coding and doesn't have much real applications where it can be used.
So if possible, I'd like to fix this my using && instead of relying on &.
I really don't understand the changes and their cause.
Thank you in advance for helping and guiding me.
~Cheers