Calculate value in array under certain condition

I have array as below:

const array = [1, 3, 5, 7, 9, 11, 8, 10, 13, 15]; 

I want to sum values less than 10 in the array to sum up and become more than 10 as below:

const newArray = [16, 20, 18, 13, 15]; // 16 at index 0 is from (1+3+5+7), 20 at index 1 is from (9+11), 18 at index 2 is from (8+10) 

This is what I have attempted and I stuck from here:

const minTen = array.reduce((accumulator, currentValue) => {          if (currentValue < 10) {          // 1. Sum value in array to become >= 10         const accumValue = currentValue += // help here please or any othr alternative          // 2. Push new value to the array         accumulator.push(accumValue);     }     return accumulator; }, []);  console.log(minTen); // [16, 20, 18, 13, 15] 
Add Comment
7 Answer(s)

Just check the last value of the result set.

const     array = [1, 3, 5, 7, 9, 11, 8, 10, 13, 15],     minTen = array.reduce((accu, value) => {         if (accu[accu.length - 1] < 10) accu[accu.length - 1] += value;         else accu.push(value);         return accu;     }, []);  console.log(minTen); // [25, 11, 18, 13, 15]

Add Comment

Steps:

  1. Iterate through the given array and give it a condition – if the number is less than 10 then sum it up, else push it into a result array.
  2. Insert the final sum in the first place of the result array.
  3. Done
const array = [1, 3, 5, 7, 9, 11, 13, 15]  let sum = 0, resultArr = [] array.forEach(i => i<10 ? sum += i : resultArr.push(i)) resultArr = [sum, ...resultArr] console.log(resultArr)

Answered on July 16, 2020.
Add Comment

array.reduce is one of the modern functions that I don’t prever to use. To be honest, I don’t know how it works. And if DBS is correct, you don’t either. So you can do it on the easy way.

for (var i=0, n=0, newArray = [];i<array.length;i++) {     if (array[i] >= 10) {         newArray.push(array[i]);     } else {         n += array[i];     } } newArray.unshift(n); 
Add Comment

If you really want to use Array.reduce :

const array = [1, 3, 5, 7, 9, 11, 13, 15]; let sum = 0; const minTen = array.reduce((acc, currentValue) => {         if (currentValue < 10) {             sum += currentValue         } else {             acc = [...acc, currentValue]         }         return acc }, []);  console.log([...minTen, sum]);

But I’m not sure it’s the best method to use.

Answered on July 16, 2020.
Add Comment

I discourage doing 2 different operations, filter and sum, in the same function. A clean solution always do one thing at a time. Here the solution is presented in the context of an automatic unit test:

test('62893326', () => {   const input = [1, 3, 5, 7, 9, 11, 13, 15]   const expected = [25, 11, 13, 15]   const lower = input.filter(i => i < 10)   const higher = input.filter(i => i >= 10)   const sum = lower.reduce((accumulator, currentValue) => accumulator + currentValue)   const result = [sum, ...higher]   expect(result).toEqual(expected) })  
Add Comment

This uses the filter function to only add the ones that are bigger than 10 and sums the lower ones. At the end the sum will just be inserted at index 0 of the array by calling Array.prototype.splice:

 const array = [1, 3, 5, 7, 9, 11, 13, 15];    let underTen = 0;  const minTen = array.filter(val => {      if (val < 10) {         underTen += val;      } else {         return val;      } });  // add sum at pos 1 minTen.splice(0, 0, underTen); console.log(minTen);

Add Comment

After experimenting for a while, I have found that this code, which mostly uses raw operations rather than library functions, sums numbers in the initial array up to form values that are greater than 10 and stops summing as soon as the sum is 11 or greater:

const array = [1, 3, 5, 7, 9, 11, 8, 10, 13, 15];  let sum = 0;  let arrayLength = array.length; for(var i = 0; i < arrayLength; i++) {     if (array[i] <= 10) {         sum += array[i];         let n = i+1;         if (n < arrayLength) {             do {                 sum += array[n];                 n++;             }             // keep adding numbers from the array until a sum of 11 or greater         while((n) < arrayLength && array[n] <= 10 && sum < 11);         }         // replace the numbers in the array that were summed together         // with the sum of those numbers.         array.splice(i, n - i, sum);         sum = 0; // reset for next iteration     }     // the for loop advances to the next position in the array automatically }  console.log(array); // outputs [16, 20, 18, 13, 15] 
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.