我的代码有效,但未被接受以通过挑战。对我做错的任何帮助将不胜感激。
挑战描述:
给定一个整数数组。 返回一个数组,其中第一个元素是正数的计数,第二个元素是负数的总和。 如果输入数组为空或 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]
返回 [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;
}
当挑战明确要求“如果输入数组为空或 null,则返回一个空数组”时,您没有检查
null
。请考虑如下更改代码
if (input == null || input.length < 1){
return [];
}
这段代码对我有用(在 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 (to sum of negatives)
这是我在 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];
}
这是我对这个任务的解决方案:
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]
}
这是我对这个任务的解决方案:
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))]
}
遇见题目中最长的代码
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;
}
这个绝对有用::
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];}
对于正数,它会将当前数加到前一个数上,并在遍历整个数组后返回最新的可用值。
这有效:
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]
}