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;
}
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;
}
Share
Improve this question
edited Feb 13, 2017 at 0:24
Eduard Malakhov
1,1144 gold badges13 silver badges25 bronze badges
asked Feb 12, 2017 at 17:02
jamesbcnjamesbcn
3474 gold badges6 silver badges21 bronze badges
9 Answers
Reset to default 3You'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 [];
}
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)
This one definitely works ::
function countPositivesSumNegatives(input) {
let positiveNums = 0;
// initialize positive number variable
let negativeNums = 0;
// initialize negative number variable
if (input === null || input.length === 0) {
return []; // if the input is empty or null, it will return empty array
} else {
input.forEach((num) => num > 0 ? positiveNums++ : negativeNums += num);
}
return [positiveNums , negativeNums];}
For positive numbers, it will add the current number to the previous number and return the latest available value after iterating through the whole array.
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];
}
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]
}
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 for codewars
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
}
}
function countPositivesSumNegatives(input) {
const pos = input.filter((el) => el >= 1).length;
const neg = input.filter((el) => el < 0);
const negSums = neg.reduce((a, b) => a + b, 0);
(input === null || input.length == 0) ? [] : (input.forEach((num) => num > 0 ? pos : []))
const finalArr = [];
finalArr.push(negSums);
finalArr.unshift(pos);
return finalArr;
}
This works:
function countPositivesSumNegatives(input) {
let pos = []
let neg = []
if (input === null || !input.length) return []
else {
input.map(num => {
return num > 0 ? pos.push(num) : neg.push(num)
})
}
let out1 = pos.length
let out2 = neg.reduce((a, c) => a + c, 0)
return [out1, out2]
}