From f4a581ab7640aaf26d28b98353cacfb98b6a9286 Mon Sep 17 00:00:00 2001 From: koronya Date: Sat, 30 May 2026 22:54:18 +0900 Subject: [PATCH] [JS][7kyu] Sum and Length --- codewars/7kyu/sum-and-length/koronya.js | 35 +++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 codewars/7kyu/sum-and-length/koronya.js diff --git a/codewars/7kyu/sum-and-length/koronya.js b/codewars/7kyu/sum-and-length/koronya.js new file mode 100644 index 000000000..40cf5eba2 --- /dev/null +++ b/codewars/7kyu/sum-and-length/koronya.js @@ -0,0 +1,35 @@ +// [JS][7kyu] Sum and Length +// sum-and-length +// https://www.codewars.com/kata/571965ccdf7fdb10a00000ea/train/javascript + +const sumLength = (array) => { + if (array.length === 0) { + return '0 0' + } + let isZeroNetgative = true + let sumOfPositive = 0 + let countOfNegative = 0 + array.forEach((num) => { + if (num > 0) { + sumOfPositive += num + } else if (num < 0) { + countOfNegative += 1 + } + if (num === 0) { + if (isZeroNetgative) { + countOfNegative += 1 + } + isZeroNetgative = !isZeroNetgative + } + }) + + return `${sumOfPositive} ${countOfNegative}` +} + +sumLength([1, 2, 3, 4, -1, -2, -3]) +sumLength([1, 2, 3, 4, 0, -1, -2, -3]) +sumLength([-1, 2, 3, 4, 0, 1, 0, -2, 0, -3]) +sumLength([-1, -2, -3, -4, 0, -1, 0, -2, 0, -3]) +sumLength([1, 2, 3, 4, 1, 2, 3]) +sumLength([0, 0, 0, 0, 0, 0, 0]) +sumLength([])