As the title says, make a classic FizzBuzz function or method without using if/else (or equivalents like ternaries, ?:, sneaky).
Specifically:
T...
For further actions, you may consider blocking this person and/or reporting abuse
I'm sure I could make a fancy function composition but my brain is satisfied with this.
Awesome! I think you're really getting at the heart of the prompt by defining
true
andfalse
as functions.this blew me away, thank you for posting it
Glad you like it. I got the idea from this talk by Anjana Vakil.
Aha! She has several functional programming in JavaScript videos I've been putting off for a while, I think this is my cue to watch one. I'd like to better understand your solution. Cheers!
Does this one count for the hard mode?
Nice solution! For hard mode, I think either do it without
.filter
or show how.filter
could be implemented according to the rules.How about with for loops?
Nice! I'll accept it even though I said no
for
loops because I like that you used the stepping statement to simply go through the multiples of 3,5,15. That's creative!I was more expecting someone to abuse the
i <= n
into then % 3
type of check in a traditional fizz buzz. (especially abusingwhile(n % 3)
into a more convolutedif
)I missed that! Sorry, but I was running out of ideas :-) Looking forward to seeing your solution(s)!
Happy Juneteenth! I love everyone's approaches here, it's fun to see the way each person thinks through the problem.
I'll be posting my answers on Monday (along with the next installment of With Only CSS) so encourage your friends to give this a shot over the weekend (or take another go yourself)!
Follow me and react with a 🔖 so you remember to come back and see what I post here.
Drop a 🦄 or ❤️ if you'd be interested in some follow up posts going more in depth into each (OO/FP) solution: how they work; how they are similar; and how they are different.
Thanks everyone who took the time to post a solution to my little challenge; I'll see you all on Monday!
As promised, here is my functional approach to solving this (deeper article on it to come soon)
Similar to Heiker's solution above; the main part of this solution is defining "true" and "false" as functions taking the same two parameters but returning one or the other.
And here is my object-oriented approach (deeper article on it to come soon)
Similarly to the functional approach; the main part of this solution is defining "true" and "false" as objects. But now instead of parameters, the objects have the same interface and return one or the other of the attributes set on the object.
You can see both solutions in action on this codepen (planning one final post doing a compare/contrast between the two approaches)
I think your
.get
is a secret conditional. (It will return the value at the givenkey
or the default IF the value is undefined)If you can show how to implement
.get
without anif
then I think this is a great answer!Do you consider something like this cheating, because it builds on
filter
, a built-in, to handle the conditional logic?Clever! I'll accept it for the normal challenge :)
If you want hard mode though, I think you'll have to show an implementation of
.filter
that doesn't useif
,?:
,||
,&&
,?.
, or??
. (I'll allow.reduce
to be used though, as well as.map
)Well done, thanks for commenting!
Okay, well, my next solution is probably not what you were expecting. It builds on the idea that a boolean can be used as a number. I think it meets the hardcore requirements.
Very nice! I think you have met the hardcore requirements.
And I think slightly less code than my functional solution will be, well done!
Well done! Clever use of
Math.sign
to select either the first or second element from an arraySlightly shorter without bothering to call out for variable potential conditions and using anons:
Still not perfect, but this one comes with a special thanks to Mr. Kevlin Henney
Nice! I like how it uses
false
andtrue
as keys on an object to select the resulting string.If you can get rid of the
||
usage, then this will meet the hard mode requirements...Well Mr. Henney has a great answer:
edit: but it still has a conditional...
Thanks for the problem. I guess this will not satisfy your conditions, but here is my solution,
It's a nice answer! It satisfies the normal rules (no
if
or?:
)Thanks for taking the time to submit a solution!
Thanks for the reply. I think re-submission is also allowed. So, here is a C++ solution, which again, may not satisfy all your conditions.
Well done! I think you've got a solution
This could probably get simplified (I've seen the one-liner of this one), but I still wanted to post mine
holy COW did I find out a lot of things about object destructuring while making this. Thanks for the challenge, Nathan. It was so great to notice how instinctively I reach for a conditional.
I'm glad you liked it! I think its good to do a little self reflection on the way we do things every once in a while.
And WOW is that an impressive use of destructuring in JS. I knew about each of those things independently; I never have thought about putting them all together like that!
A bit late to the party...
Welcome to the party!
I am posting for a friend who found some absolutely mind blowing ideas.
First a one-liner that extends my destructuring idea.
secondly a solution built around a regex from hell/heaven
You are referring to the following clause:
My understanding is that this applies to the usage of such operators as conditionals when they are applied to non-boolean types. For example:
In my code, it is used strictly on Boolean values, which I don't think was against the rules.
It uses it in a strictly boolean sense, which I'll allow. What isn't allowed is the loose/early-return way JS can use
&&
; a contrived example that would not pass hard mode:(an easy litmus test: does changing the order of the
&&
expression change the result?)Has someone done this yet? It's really very fast.
There's a ternary there, I admit.
Nice! But
test
uses a ternary?:
(which is just a different way to spellif
in my opinion)If you can get rid of that I think you'll have a solution!
I can't bring myself to be terribly interested in re-implementing language features like boolean or ternary. I loved seeing some of the really outlandish solutions like that un-sane RegEx that was posted. I enjoyed seeing the contortions done to avoid conditionals, too, but that's not something I'm into for it's own sake. I'm happy to fail at it.
There is a meaningful distinction between ternary and
if
.if
is a wilderness with few boundaries, as a form of Many-Valued Logic with an arbitrary number of values. Ternary is a specific subset of Many-Valued Logic; Three-Valued Logic. The Three-Valued Logic of ternary operators provides specific limitations that are not necessarily implied by the N-Valued Logic of theif
statement. In most programming languages ternary is an operator, andif
is a control structure block. Operators can be used in expressions, which is generally not true ofif
statements. Ternary is usually slightly computationally more expensive thanif
but has some advantages in terms of limiting complexity.In real-world code I'm going to write an
if
(withoutelse if
orelse
if I can help it) about 95% of the time. It is both more readable and more performant in most cases. But when I need an expression that can make a decision between boolean and a maybe, I reach for ternary.If you wanted to argue that
switch
is basicallygoto
but potentially worse due to unintended fallthrough, then we'd have something to agree on.The magic of the fizzbuzz problem is, of course, that it helps us understand in more granular detail that our design decisions, and design non-decisions, come with unavoidable trade-offs. It is a meditation on (a horrible term)
non-functional requirements
.Very nice! I feel like this is actually pretty similar to my own Object-oriented approach (if you wanted to read):
Unconditional Fizzbuzz: an Object-oriented approach
Nathan Kallman ・ Jul 6 '20 ・ 3 min read
How about this:
Nice!
Excellent! I think you've checked off all the boxes. Thanks for submitting!
I'm thinking of an absolutely egregious version using try and catch, but I'm going to go ahead and assume that try/catch would be a "surrogate conditional"
😂 it probably is, but now I want to see it implemented with just try/catch!