Codewars Challenge - Count of positives / sum of negatives

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Codewars Challenge - Count of positives / sum of negatives



My code works but it's not being accepted in order to pass the challenge. Any help on what I'm doing wrong would be appreciated.



Challenge Description:



Given an array of integers.
Return an array, where the first element is the count of positives numbers and the second element is sum of negative numbers.
If the input array is empty or null, return an empty array:


C#/Java: new int / new int[0];
C++: std::vector<int>();
JavaScript/CoffeeScript/PHP/Haskell: ;
Rust: Vec::<i32>::new();



ATTENTION!
The passed array should NOT be changed. Read more here.*



For example:



input [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, -11, -12, -13, -14, -15]



return [10, -65].



My Code:


function countPositivesSumNegatives(input)

if (input.length < 1)
return ;


var newArray = [0, 0];


for (var i = 0; i < input.length; i++)

if (input[i] > 0)

newArray[0] += 1;


else
newArray[1] += input[i];



return newArray;




3 Answers
3



You're not checking for null when the challenge explicitly requires that "if the input array is empty or null, return an empty array". Please consider changing the code as follows


null


if (input == null || input.length < 1)
return ;



This code is working for me (in JavaScript)


function countPositivesSumNegatives(input)
if (input === null



So, you need check if input === null (and return empty array), and if input[i] <= 0 (to sum of negatives)



Here is an approach I used in Javascript so maybe you can borrow afew ideas off it as well


function countPositivesSumNegatives(input)






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard