Facebook Pixel

Count Negative

JavaScript
easy
10 mins

Given an array arr of numbers, return the count of elements strictly less than 0.

Examples

Input: arr = [-1, 0, 1] Output: 1
Input: arr = [-2, -5, -7] Output: 3
Input: arr = [0, 2, 3] Output: 0
Input: arr = [] Output: 0

Constraints

  • Input must be an array of finite numbers
  • Return false for non-array inputs
  • Return false for arrays containing non-number values
  • Return false for arrays containing NaN, Infinity, or -Infinity

Function Signature

function countNegatives(arr) { // Your code here }

Test Cases

  • Base cases: empty array [], single negative [ -1 ], single non-negative [ 0 ]
  • Mixed arrays: [ -1, 0, 1 ], [ -5, 2, -3, 4 ]
  • All negatives: [ -1, -2, -3 ]
  • No negatives: [ 0, 1, 2 ]
  • Decimals: [ -1.5, -0.1, 0, 2.2 ]
  • Invalid inputs: null, undefined, 42, '8', {}, () => {}, [1, 'a'], [NaN], [Infinity]

Companies:

Accenture
amazon
tcs

Solve Similar questions šŸ”„

Please Login.
Please Login.