samedi 28 décembre 2019

logical operators and if statements combination

I'm looking if there is an alternative to write this code cleaner and efficient. My goal is to set different values based on the hour of the day.

var hour = new Date().getHours();
var user = 'User4';

if (hour >= 4 && hour < 6) {
    user = 'User1';
}
if (hour >= 6 && hour < 13) {
    user = 'User2';
}
if (hour >= 13 && hour < 19) {
    user = 'User3';
}
if (hour >= 19) {
    user = 'User4';
}

I found this article online https://blog.wax-o.com/2015/05/an-alternative-to-if-else-and-switch-in-javascript/ but I couldn't achieve logical operators into this.

let values = {
    a: 1,
    b: 2,
};
let foo = values[ bar ] || 3;

Really appreciate your help! Thanks 😗

Aucun commentaire:

Enregistrer un commentaire