*Each week, I offer up a JavaScript code challenge. Want more? You can find others here.*

This week’s code challenge comes from Coderbyte. Here are the basic instructions:

Have the function `StringReduction(str)` take the `str` parameter being passed and return the smallest number you can get through the following reduction method. The method is: Only the letters a, b, and c will be given in str and you must take two different adjacent characters and replace it with the third.

For example, if you were provided the string “abc,” you could simplify that to “cc” but no further. On the other hand, “aba” could be simplified to “ca” and then to “b.” For simplification, here are all of the transformations:

`ac = b`

ca = b

cb = a

bc = a

ab = c

ba = c

Here are some test cases:

StringReduction(“abcabc”) should equal 2.

StringReduction(“cccc”) should equal 4.

StringReduction(“baccabcbc”) should equal 1.

Here’s my solution collapsed: