• + 4 comments

    Hi, The logic here is based on finding out the minimum number of jumps that are required to reach the last cloud Cn-1. Since there are 'n. elements in the array and she starts at C[0], we need to loop (while loop in this case) through until the index ( initialized at 0) reaches n-1 to avoid out of bound error. To minimize the number of jumps, ideally she should jump two clouds at a time before reaching the destination Cn-1 cloud. However in jumping two clouds at a time, she should not, by mistake land on a thunder cloud. A thunder cloud is an element with a value of 1 assigned to it. So with the ' if ' statement we check if every alternate cloud (skipping the adjacent cloud) is not a thunder cloud i.e. C[index +2] !== 1. Also the ' if ' statement checks if the current jump does result in jumping over the very last cloud Cn if that happens to be an adjacent cloud. If these two conditions are met, then a jump skipping the adjacent cloud and landing on the one next to it is made. To signify this permitted 'double jump;, the index is advanced by +2. The number of jumps incremented by one. If either one of the conditions are not met (the else clause), only a single jump is made with her landing on the adjacent cloud. So the index is incremented by just 1 and the number of jumps incremented by one.

    When the while loop comes to an end, number of jumps made is returned by the function.

    Though this code works and passes every test, I am not happy with it because, I get a notice that an undefined offset is reached at the end of the trip. May be I need to rewrite or improve upon the code with verifying if the cloud C[index +2] does really exist in every cycle of the while loop before making the jump i.e. using the isset(C[index +2]) function.