javascript Codewars挑战-阳性计数/阴性总和

qv7cva1a  于 2023-05-05  发布在  Java
关注(0)|答案(9)|浏览(315)

我的代码工作,但它不被接受,以通过挑战。如果你能告诉我我做错了什么我会很感激的。

挑战说明:

给定一个整数数组。返回一个数组,其中第一个元素是正数的计数,第二个元素是负数的和。如果输入数组为空或null,则返回一个空数组:

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

注意!传递的数组不应该被改变。在这里阅读更多。*

例如:
输入[1,2,3,4,5,6,7,8,9,10,-11,-12,-13,-14,-15]
return[10,-65]。
我的密码:

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;
}
qxsslcnc

qxsslcnc1#

当challenge明确要求“如果输入数组为空或null,则返回一个空数组”时,您没有检查null。请考虑更改代码如下

if (input == null || input.length < 1){
  return [];
}
fjaof16o

fjaof16o2#

这段代码是为我工作(在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;
}

因此,需要检查input === null(并返回空数组),以及input[i]〈= 0(对负数求和)

vom3gejh

vom3gejh3#

下面是我在Javascript中使用的一种方法,也许你也可以从中借鉴一些想法

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];
}
cbjzeqam

cbjzeqam4#

以下是我对这个任务的解决方案:

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]
}
byqmnocz

byqmnocz5#

以下是我对这个任务的解决方案:

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

kninwzqo

kninwzqo6#

满足主题中最长的代码

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
    }
}
d6kp6zgx

d6kp6zgx7#

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;

}

dgtucam1

dgtucam18#

这一个绝对有效::

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];}

对于正数,将当前数与前一个数相加,遍历整个数组后返回最新的可用值。

ubof19bj

ubof19bj9#

这是可行的:

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]

}

相关问题