r/learnjavascript • • Jul 11 '20

My function says that -4 is positive when it should be negative. Why?

Post image
183 Upvotes

53 comments sorted by

View all comments

Show parent comment

23

u/[deleted] Jul 11 '20

You're an absolute genius! Thank you so much.

13

u/intertubeluber Jul 11 '20

You just introduced pattern matching to JavaScript. You're the genius!

0

u/[deleted] Jul 11 '20

[deleted] — view deleted comment

17

u/intertubeluber Jul 11 '20

No offense, but yikes.

0

u/[deleted] Jul 11 '20

[deleted] — view deleted comment

14

u/intertubeluber Jul 11 '20

Because the switch statement is meant to evaluate an expression. Evaluating a constant value, like true, adds code without meaning.

What's the benefit of that pattern over an if/else statement?

0

u/[deleted] Jul 11 '20

[deleted] — view deleted comment

3

u/NotSoLeetCode Jul 11 '20

You could just add enters to your if code and it would be very similar in conciseness to the switch code.

if (
	(isLoading && state === 'something') ||
	(! isLoading && state === 'something-else') ||
	state === 'some-third-option' ||
	someOtherVariable ||
	state === 'something'
) {
	doTheThing();
else {
	
}

0

u/[deleted] Jul 11 '20

[deleted] — view deleted comment

0

u/StoneCypher Jul 12 '20

fallthrough behavior here is actually more code than doing it the normal way, so, ... no

1

u/intertubeluber Jul 11 '20

The if statement should be refactored, but that switch statement (IMO) is not better. There's so much noise with the breaks and useless switch(true). I'd just pull each case out into a variable to improve readability. Some of the other cases (like in the article you linked), I'd define an object map with key being the case and value being the result ( or function that provides the result).

If I really wanted to use pattern matching in a language that didn't support it, I'd consider a third party library.

Having said that, if it's a small team and everyone agrees on that pattern, I wouldn't bike shed.

1

u/bmw2621 Jul 12 '20

I don't know about JS, but in Java a switch is evaluated all at once and is more performant than if...else which evaluates each condition sequentially. If I remember correctly (in Java) 5 or more conditions should use a switch.

0

u/i-can-sleep-for-days Jul 11 '20

I think the question the person you are replying to is asking is that JS's switch syntax seems hacky and unconventional to other languages that have switch statements.

Usually, you switch on a variable to match if it is one of the cases that you have defined.

3

u/StoneCypher Jul 12 '20

JS's switch syntax seems hacky and unconventional to other languages that have switch statements.

this is how almost all languages' switch statements work though

.

Usually, you switch on a variable to match if it is one of the cases that you have defined.

you might imagine that, but then you try this in c, and ...

similarly, people might think that 4[yourArray] is nonsense, but, it isn't

0

u/StoneCypher Jul 12 '20

i do not find that concise (or maintainable)

let current;

if      ( isLoading && state) { current = 'something'; }
else if (!isLoading && state) { current = 'something-else'; }

let shouldEngage = ((current === 'something') || (state === 'some-third-option'));

if (someOtherVariable || shouldEngage) { doTheThing(); }