Loading collection data...
Collections are a way for you to organize kata so that you can create your own training routines. Every collection you create is public and automatically sharable with other warriors. After you have added a few kata to a collection you and others can train on the kata contained within the collection.
Get started now by creating a new collection.
I agree with @ewhoambra, the solution will fail when implementing it for "Hello I am Joe".
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 fromnum
to1
.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.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?