mardi 19 février 2019

Why is my If-Else statement defaulting to "else"?

I'm new to JavaScript and have just been toying with this little IF-ELSE exercise. Essentially, slots 1 thru 4 are static to experiment with the || operator. The const 'testSlot' is one that I've altered as time passes to try and execute the "else if" segment of my code; e.g, if it's 9:10 PM I've just been manually changing the getHours to 21 and the getMinutes to 10 and then run the code.

With that said, I can't get either of the first two console.logs to run, it always just runs the "else", which is three.

My question is mostly if my usage the of the date object was wrong or if the syntax in my If-Else statement was wrong. A pointer in the right direction would be very much appreciated.

Here is my code:

const now = new Date();
const slot1 = now.getHours === 12 && getHours.getMinutes === 1;
const slot2 = now.getHours === 12 && getHours.getMinutes === 2;
const slot3 = now.getHours === 12 && getHours.getMinutes === 3;
const slot4 = now.getHours === 12 && getHours.getMinutes === 4;

const testSlot = now.getHours === 20 && getHours.getMinutes === 34;

if (slot1 || slot2 || slot3 || slot4) { 
  
  console.log('one');
  
} else if (testSlot) {  
  
  console.log('two'); 
  
} else {  
  
  console.log('three');
  
};

Aucun commentaire:

Enregistrer un commentaire