Problem 35

Alternate sign Recursion Challenge

Given an array of numbers, return a new array where the signs alternate: the first value is positive, the second is negative, the third is positive, and so on. Use the absolute value of each element, then apply the appropriate sign based on its position.

Function Signature

alternateSign(array)

Parameters

  • array — an array of numbers.

Output

Return a new array with alternating positive and negative values.

Constraints

  • The function must use recursion.
  • The input array must not be mutated.
  • The function should accept exactly one argument.

Examples

alternateSign([2, 7, 8, 3, 1, 4])      → [2, -7, 8, -3, 1, -4]
alternateSign([-2, -7, 8, 3, -1, 4])   → [2, -7, 8, -3, 1, -4]