← Back to challenges

Positives and Negatives

JavaScriptHardarraysnumbersloopsvalidation

Instructions

Create a function which validates whether a given array alternates between positive and negative numbers.

Examples

alternatePosNeg([3, -2, 5, -5, 2, -8]) ➞ true

alternatePosNeg([-6, 1, -1, 4, -3]) ➞ true

alternatePosNeg([4, 4, -2, 3, -6, 10]) ➞ false

Notes

  • Lists can be of any length.
  • It doesn't matter if an array begins/ends with a positive or negative, as long as it alternates.
  • If an array contains 0, return false (as it is neither positive nor negative).
javascript
Loading editor…
to run
Walks through the solution with reasoning and edge cases.