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.
- Initialize
resultwith the first element (index 0). - Loop through the rest of the array.
- If the current index is odd, subtract the value.
- If the current index is even, add the value.
- Print the final result.
01EXAMPLE 1
Input
[10, 2, 5]Output
13Explanation: 10 - 2 + 5 = 13.
Constraints
- Handle single element arrays (return element).
ArraysLoops
JavaScriptSystem handles I/O — write your function only
Loading...
2 Hidden
Input Arguments
sequence[10,2,5]
Expected Output
13
Click RUN to test your solution