Ad
  • Custom User Avatar

    I agree with @ewhoambra, the solution will fail when implementing it for "Hello I am Joe".

  • Custom User Avatar

    Each time function shortestStepsToNum(num) is called recursively, num gets updated (by dividing by 2). So yes - num decreases with each recursive call, until the base condtition is met (if (num < 3)). We go backwards from num to 1.
    Be careful with these operators: increment ++ decrement --. They increase and decrease by 1. In this kata, num gets decreased not by 1, but by assigning it new value === num/2.
    I guess the "ticks++" you mentioned, in this very solution this operation takes place here: return 1 + shortestStepsToNum(next);.
    return 1 adds a step with each recursive call.

  • Custom User Avatar

    Could someone help me undertsand that ternary expression, please.

    If num is divisable by 2.
    the value of num will be num divided by 2
    then i don't quite understand.

    For instance
    given num = 12
    it would get into the while for the first time (12).
    12 % 2 == 0? true
    num = 12 / 2
    num = 6
    since it was true. It would decrease num-- otherwise it would increase the ticks++.

    Is that correct?