A few minor notes:
1.
Have the function StringReduction(str)
strictly speaking, the parameter name is different:
function StringReduction(s)
:-)
1.
var original, key;
I'd put the variable declarations to separate lines. From Code Complete 2nd Edition, p759:
With statements on their own lines, the code reads from top to bottom, instead of top to bottom and left to right. When you’re looking for a specific line of code, your eye should be able to follow the left margin of the code. It shouldn’t have to dip into each and every line just because a single line might contain two statements.
It could prevent some diff/merge conflicts (you modify the first variable, a coworker the second one at the same time).
keycould be declared inside thewhileloop.
- JavaScript variables declare outside or inside loop?JavaScript variables declare outside or inside loop?
- Effective Java, Second Edition, Item 45: Minimize the scope of local variables
As @konijn pointed out in his comment, that may confuse maintainers. Anyway, extracting it out into a separate function solves the issue (#6).
The name
originalcould belastStringorlastValuesince it's not the original string, it's the result of the last loop.Indentation is not consistent. Most of the places it's two spaces but the
whileloop is only one space indented, the body of theforloop has three spaces.I'd extract out the
forloop to astring translate(string, replacements)function for better readability.