JavaScript

strategy pattern in JavaScript

JavaScriptDesign PatternsStrategy patternadvanced

What this snippet does

Strategy replaces conditionals — instead of if-else on algorithm type, you swap in a different function at runtime.

Expected output

[ 1, 1, 3, 4, 5, 9 ]
[ 9, 5, 4, 3, 1, 1 ]

Why practise typing this

Typing design patterns code builds muscle memory for the symbols and indentation JavaScript uses most, which prose-based typing tests never cover. DevType measures your words per minute and accuracy on this snippet and tracks the individual characters you mistype, so later lessons can target them.

More JavaScript practice

Browse all JavaScript snippets →