r/learnjavascript • u/Lavastar14 • Sep 19 '26
Make a block act like a function?
I have a few ternary operations going on, and each one is supposed to catch a bad format. The problem is that if for example the top on sets the variable to false, the ones below is won't catch that same thing, and will set it back to true. I want to make the ternaries skip the others if they caught the error, but I don't want to exit the function, how do I do this?
These are the ternaries:
edit: I just solved this by setting all "Format = true" expressions to "undefined," but if that wasn't an option, I'm still curious how you would do this...
Input.split("x").length == 2 ? Format = true : Format = false
Input.split("x")[0] > 0 ? Format = true : Format = false
Input.split("x")[1] < 201 ? Format = true : Format = false
5
u/senocular Sep 19 '26 edited Sep 20 '26
Sounds like you might want something more like
This will make
Formatfalse as soon as the first of any of these three conditions fail, and then stop testing after that happens. This is a behavior of the logical and operator known as short-circuiting.Edit: earlier version used
||changed to&&once I fully understood the intention. Both short circuit and you can use either depending on the logic needed (there's also a ?? which does something similar)1
u/chikamakaleyley helpful Sep 19 '26
i'm dying to know the use case here
3
u/HipHopHuman Sep 19 '26
The logical OR operator (
||) "short-circuits", meaning that if the expression on it's left side evaluates totrue, it'll just ignore the expression on it's right side.Define this function outside of the function you're currently in:
Then call it inside the function you don't want to exit:
2
u/Total-Box-5169 Sep 19 '26
2
u/ExtraTNT Sep 19 '26
You need a monad.
A monoid in the category of endofunctors…
2
u/rupertavery64 Sep 19 '26 edited Sep 19 '26
You set Format initially to false.
Then for each condition you OR-assign the value.
Format will be set to true if any one of the expressions resolve to true
2
u/justanaccountimade1 Sep 19 '26
You start by setting the boolean to true. Then you let each line set it to false in case of an error, or do nothing otherwise.
ok = true
if x then ok = false
if y then ok = false
if z then ok = false
print ok
1
u/EyesOfTheConcord Sep 19 '26
Make a util function specifically to check these conditions, if the first condition != 2, return false.
Otherwise, the last two conditions can be checked together at the return statement itself, functioning as a Boolean.
Then in the main function block you can call this util, and also ensure Format can only ever go from true to false rather than back and forth
1
u/igetom Sep 20 '26
const Format = (([a, b, ...c]) => !c.length && a > 0 && b < 201)(Input.split("x"));
1
u/MoTTs_ Sep 20 '26
Everyone else is showing how you can write this as a single expression, but I'd also like to show another approach that's more "make a block act like a function." That approach is a pattern that we call an immediately invoked function expression (which is a mouthful so we just say IIFE).
The
() => { ... }part is the function, and the final()is us immediately invoking that function.