'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;
}
Solution 1:[1]
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
if (input == null || input.length < 1){
return [];
}
Solution 2:[2]
This code is working for me (in JavaScript)
function countPositivesSumNegatives(input) {
if (input === null || input.length < 1) {
return [];
}
var array = [0, 0];
for(var i = 0; i < input.length; i++) {
if(input[i] <= 0) {
array[1] += input[i];
} else {
array[0] += 1;
}
}
return array;
}
So, you need check if input === null (and return empty array), and if input[i] <= 0 (to sum of negatives)
Solution 3:[3]
Here is an approach I used in Javascript so maybe you can borrow afew ideas off it as well
function countPositivesSumNegatives(input) {
if (input == null || input.length < 1){
return [];
}
var sum =0;
var pos =[];
for (var i=0; i<input.length; i++){
if(input[i]>0){
pos.push(input[i]);
} else{
sum += input[i];
}
}
return [pos.length, sum];
}
Solution 4:[4]
Here is my solution for this task:
function countPositivesSumNegatives(input) {
let sumOfPositive = 0;
let sumOfNegative = 0;
if(input == null || input.length < 1) {
return [];
} else {
input.map(item => {
if(item > 0) {
sumOfPositive++;
} else if(item < 0) {
sumOfNegative += item;
} else {
return []
}
})
}
return [sumOfPositive, sumOfNegative]
}
Solution 5:[5]
Here is my solution for this task:
function countPositivesSumNegatives (a) {
if (!a || !a.length) return []
let pos = a.filter(x => x > 0),
neg = a.filter(x => x <= 0)
return [pos.length, Math.floor(neg.reduce((s,v)=>s+v,0))]
}
Solution 6:[6]
Meet the longest code in the topic
function countPositivesSumNegatives(input) {
if (input && input.length > 1) {
let count = [];
let sum = [];
for (i=0; i<input.length; i++) {
if (input[i] > 0) {
count.push(input[i]);
} if (input[i] < 0) {
sum.push(input[i]);
}
};
let sumNegatives = 0
for (i=0; i<sum.length; i++) {
sumNegatives += sum[i];
}
let result = [count.length, sumNegatives];
return result;
};
if (input === null || input.length < 1) {
let result = [];
return result;
};
if (input[0] < 0) {
let result = [0, input[0]]
return result
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Eduard Malakhov |
Solution 2 | Valdemar26 |
Solution 3 | cherucole |
Solution 4 | tommyahav10 |
Solution 5 | |
Solution 6 | ????????? ????? |