Skip to content
Trace code in your head

Module challenge

Challenge: trace code

15 min · 3 steps

  1. Step 1 of 3 · Write the code

    solution(n)

    Return the sum of all digits of the integer n.

    Example: solution(507) returns 12.

    Constraints:

    • 0 ≤ n ≤ 10⁹
    function solution(n) { }
  2. Step 2 of 3 · Write the code

    solution(s)

    Return how many positions i have s[i] equal to s[i + 1].

    Example: solution("aabccc") returns 3 (one from aa, two from ccc).

    Constraints:

    • 0 ≤ s.length ≤ 1000
    • s only contains lowercase letters
    function solution(s) { }
  3. Step 3 of 3 · Write the code

    solution(nums)

    Return the sum of the numbers in nums before the first negative number. That negative number and everything after it don't count. If there's no negative number, add them all.

    Example: solution([3, 4, -1, 10]) returns 7.

    Constraints:

    • 0 ≤ nums.length ≤ 1000
    • -1000 ≤ nums[i] ≤ 1000
    function solution(nums) { }