The Alternator

HardAcc. 76.4%
+40 XP 20

Mathematical Rhythm

The Alternator sum is defined as: $a_0 - a_1 + a_2 - a_3 dots$.

The Assignment

Your function receives an array sequence.

  1. Initialize result with the first element (index 0).
  2. Loop through the rest of the array.
  3. If the current index is odd, subtract the value.
  4. If the current index is even, add the value.
  5. Print the final result.

01EXAMPLE 1

Input[10, 2, 5]
Output13

Explanation: 10 - 2 + 5 = 13.

Constraints

  • Handle single element arrays (return element).
ArraysLoops
JavaScript
Loading...
2 Hidden

Input Arguments

sequence[10,2,5]

Expected Output

13

Click RUN to test your solution