DEV Community

Discussion on: Daily Coding Puzzles - Oct 29th - Nov 2nd

Collapse
 
dance2die profile image
Sung M. Kim • Edited

Solved it awhile ago (forgot about it).

Here is a C# answer.

using System.Linq;
using System;

public class Kata
{
    public static int[] CountPositivesSumNegatives(int[] a)
    {
        // guard clause for edge cases
        if (a == null || a.Length == 0) return new int[0];

        int count = 0;
        int sum = 0;
        // ".ToList()" is required to iterate the sequence
        a.Select(n => n > 0 ? count++ : sum += n).ToList();

        return new[] {count, sum};
    }
}

And just re-solved it using JavaScript

function countPositivesSumNegatives(input) {
    return (input && input.length >= 1) 
      ? input.reduce((acc, n) => {
          n > 0 ? acc[0]++ : acc[1] += n;
          return acc;
        }, [0, 0]) 
    : [];
}