JavaScript JavaScript: Different Results with && and &, with numbers javascript sort different results,javascript regex different results

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

Answer:1

You need to check the player against both of the others like this: The way you have it now your if statement is Steve > John(true) && Mark(true).

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 && John > Steve ) {
console.log('John wins!');
} else if (Mark > Steve && Mark > John) {
console.log('Mark wins!');
} else if (Steve > John && Steve > Mark) {
console.log('Steve wins!');
} else {
console.log("it's a draw.");
}
Answer:2

You can do this, you just have to make an improved > operator that suits your needs. But please stay tuned until the end of this answer. We'll address some other things that we really need to talk about

const gtAll = (x, y, ...ys) =>
  ys.length === 0
    ? x > y
    : x > y && gtAll (x, ...ys)

console.log ('5 is greater than 4, 3, and 2', gtAll (5, 4, 3, 2))
// true

console.log ('5 is greater than 4, 3, and 7', gtAll (5, 4, 3, 7))
// false
Answer:3

Hi I want to use a similar functionality like : Using Inline Templates in Angular But in my case url will be dynamic. for example in below code url will have /first or /second but in my case it will ...

Hi I want to use a similar functionality like : Using Inline Templates in Angular But in my case url will be dynamic. for example in below code url will have /first or /second but in my case it will ...

  1. inline template angular 8
  2. inline template angular
  3. inline template angular 6
  4. inline template angular 4
  5. inline template angular 2
  6. inline template angular 7
  7. inline template angular component
  8. inline template angular 2 example
  9. inline style angular template
  10. angular inline template example
  11. angular inline template vscode
  12. angular inline template variable
  13. inline javascript in angular template
  14. angular inline template directive
  15. angular inline template command

I created a simple login page, which, once the button is pressed, executes this function : login = (email, password, navigate) => { this.setState({ loginButtonPressed: true }); firebase ....

I created a simple login page, which, once the button is pressed, executes this function : login = (email, password, navigate) => { this.setState({ loginButtonPressed: true }); firebase ....

  1. undefined the function

I need to show the caption div when current item is focused. i am able to show the caption but when second last image focused with cation last image also show caption in loop. In first loop all ...

I need to show the caption div when current item is focused. i am able to show the caption but when second last image focused with cation last image also show caption in loop. In first loop all ...

I often read that I should not use HTML's onclick attribute because it decrease code readability. Instead I should make event listener in my script.js. So what do you think about v-on:click? Is it ...

I often read that I should not use HTML's onclick attribute because it decrease code readability. Instead I should make event listener in my script.js. So what do you think about v-on:click? Is it ...