Herman93
Herman93

Reputation: 21

How to shorten this if else statement?

I'm beginning to understand JavaScript now but I try to understand how I can shorten a if else statement like this.

let today = prompt('Enter day').toLowerCase();

if(today === 'monday' || today === 'tuesday' || today === 'wednesday' || today === 'friday') {
    console.log(`${today.replace(/^\w/, (c) => c.toUpperCase())} is a work day.`);
} else if(today === 'saturday' || today === 'sunday') {
    console.log(`${today.replace(/^\w/, (c) => c.toUpperCase())} is weekend day.`);
}

Upvotes: 0

Views: 76

Answers (5)

Kenny
Kenny

Reputation: 315

Check out the includes() method.

let today = prompt('Enter day').toLowerCase();

if('monday tuesday wednesday friday'.split(' ').includes(today)) {
    console.log(`${today.replace(/^\w/, (c) => c.toUpperCase())} is a work day.`);
} else if(['saturday', 'sunday'].includes(today)) {
    console.log(`${today.replace(/^\w/, (c) => c.toUpperCase())} is weekend day.`);
}

In the first use above, we also employ split() in order to condense the list a little bit. This only really helps with particularly long lists. And there is a performance hit the longer the string is, so this is just to give you an idea of some options that might work for you if it is a rarely used piece of code.

In the weekend case, an explicit list is defined without split(). I find this approach better for small lists. Predefining the list may help with readability and anywhere that the comparison is used multiple times. Ex:

const weekend = ['saturday', 'sunday'];
if (weekend.includes(today)) ...

Upvotes: 0

Lakshaya U.
Lakshaya U.

Reputation: 1171

My approach

<!DOCTYPE html>
<html>
    <body>
        <script type="text/javascript">
            const today = prompt("Enter day");
            const days = {weekdays: ["monday", "tuesday", "wednesday", "thursday", "friday"], weekends: ["saturday", "sunday"]}
            days.weekdays.includes(today) ? document.write(`${today.replace(/^\w/, (c) => c.toUpperCase())} is a work day.`) : (days.weekends.includes(today) ? document.write(`${today.replace(/^\w/, (c) => c.toUpperCase())} is weekend day.`) : console.log(`${today} is not a day`));
        </script>
    </body>
</html>

Upvotes: 2

Safir Motiwala
Safir Motiwala

Reputation: 121

let today = prompt('Enter day').toLowerCase();

let weekdays = ['monday', 'tuesday', 'wednesday', 'friday']
let weekends = ['saturday', 'sunday']

let dayString = weekdays.includes(today) ?  ` is a work day.` : weekends.includes(today) ? ` is weekend day.`: '';
let finalString = `${today.replace(/^\w/, (c) => c.toUpperCase())}` + dayString;

console.log(finalString);

Upvotes: 2

Thomas Junk
Thomas Junk

Reputation: 5676

I would use regular expressions

if(/monday|tuesday|wednesday|thursday|friday/.test(today))

https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Global_Objects/RegExp/test

Upvotes: 0

koalaok
koalaok

Reputation: 5720

Assuming here that we don't have much info from you, and this kind of snippets could have some other issues before being considered "solid"

Just for the sake of the question... I'd make the snippet shorter like this:

let workOrWeekendDay =
today === "saturday" || today === "sunday" ? "weekend" : "a work";

console.log(
`${today.replace(/^\w/, (c) => c.toUpperCase())} is ${workOrWeekendDay} day.`
);

Upvotes: 1

Related Questions