Sum Consecutives HELP improve my code

challenge : https://www.codewars.com/kata/sum-consecutives/train/javascript
Please help improve my code… i manage to pass the test but i feel its not dynamic :confused: i find this test pretty difficult. please help!

my code:

function sumConsecutives(s) {
  for( let i = 0; i < s.length; i++) {

    if (s[i] === s[i+1] && s[i] === s[i+2] && s[i] === s[i+3] && s[i] === s[i+4]) { 
      var duplicateIndex = i;
      var arr = [];
      for ( let j = duplicateIndex; j < s.length; j++) {
        if (s[j] !== s[i]) { 
          break;
        } 
arr.push(s[j])
      }
console.log(arr)
let reduced = arr.reduce(function(a,b) { 
return a + b;
});
console.log(reduced)
 s.splice(i,5, reduced)
    }
    ///////////////////////////////////////////////////
if (s[i] === s[i+1] && s[i] === s[i+2] && s[i] === s[i+3]) { 
      var duplicateIndex = i;
      var arr = [];
      for ( let j = duplicateIndex; j < s.length; j++) {
        if (s[j] !== s[i]) { 
          break;
        } 
arr.push(s[j])
      }
console.log(arr)
let reduced = arr.reduce(function(a,b) { 
return a + b;
});
console.log(reduced)
 s.splice(i,4, reduced)
    }
////////////////////////////////////////////////////
    if (s[i] === s[i+1] && s[i] === s[i+2]) { 
      var duplicateIndex = i;
      var arr = [];
      for ( let j = duplicateIndex; j < s.length; j++) {
        if (s[j] !== s[i]) { 
          break;
        } 
arr.push(s[j])
      }
console.log(arr)
let reduced = arr.reduce(function(a,b) { 
return a + b;
});
console.log(reduced)
 s.splice(i,3, reduced)
    }
  ////////////////////////////////////////////////////////
if (s[i] === s[i+1] && s[i] !== s[i+2]) { 
      var duplicateIndex = i;
      var arr = [];
      for ( let j = duplicateIndex; j < s.length; j++) {
        if (s[j] !== s[i]) { 
          break;
        } 
arr.push(s[j])
      }
console.log(arr)
let reduced = arr.reduce(function(a,b) { 
return a + b;
});
console.log(reduced)
 s.splice(i,2, reduced)
    }
///////////
  }

return s;
}
// sumConsecutives([1,4,4,4,0,4,3,3,1])
sumConsecutives([2,2,-4,4,5,5,6,6,6,6,6,1])

// [1,4,4,4,0,4,3,3,1] # should return [1,12,0,4,6,1]

you are doing a lot of algorithms in a short time, maybe take a bit more time to review each one, before asking for help?

i’ve always put at least few hours on each one but i was advised to spend 20 mins max on each. i can’t be doing this because its detrimental to my goal. sometimes i need lil bits of tips from separate minds that im unable to spot. then i come up with the new solution myself. u don’t have to give me a full answer

you have posted a working solution here, what help do you need with this?

as this is a working solution I’m blurring it out
Your code has been blurred out to avoid spoiling a full working solution for other campers who may not yet want to see a complete solution. In the future, if you post a full passing solution to a challenge and have questions about it, please surround it with [spoiler] and [/spoiler] tags on the line above and below your solution code.

Thank you.

What’s your goal though? The point of katas is to train certain techniques through repetition, you’re supposed to be able to work on them over and over again rather than just bang them out, it isn’t a race. The “spend 20 minutes, then ask for help thing” is genuinely something I would advise, but specifically for production code (where time is imperative). In relation to this, for specific parts of the kata, not the whole thing.

That being said: Your code at the minute is extremely complicated and you can do this in a single loop. You need to start breaking things down a bit more into smaller parts.

  1. Input is an array of numbers
  2. The array is not ordered
  3. Output is an array of numbers with the sums of identical numbers.
  4. Because the array is not ordered, you could sort it. That will then be the input to a loop.
  5. You’re going to need to loop through the array, and you can just have a single loop for the whole function.
  6. You know you’re going to loop over input and return a new array with output,so maybe set up an empty array and return that populated.
  7. Say you have an array like [1], output will be [1]. So now you loop over input array and push values to output array.
  8. [1, 1] will be [2], so now the loop needs to add the input values then push the result. So you’ll need some variable to hold onto the sum.
  9. Say you have an array like [1, 2], you output [1] on first loop iteration, but now you can’t just add the next value. So you before you add, you need to check if the current value is different to the previous value. If it is, push the sum and start adding up again. If not, add to the sum.
  10. At this point this particular solution to the kata should work for all test inputs.

This is one solution, there are many many ways to do this (you don’t need to sort for example, or you can just use a single reduce function, maybe you keep a map of values you’ve used, etc etc)

nvm checked the test cases its just a complete mess full of errors

any idea why im unable to splice out the non consecutives?

function sumConsecutives(s) {
  
  let arr = [];
    
for ( let i =0; i < s.length; i++) { 
  if (s[i] !== s[i-1] || s[i] !== s[i+1]) { 
    arr.push(s.splice(i,1))
    i--;
  }
  
}


    console.log(arr)
    console.log(s)
}

sumConsecutives([1,4,4,4,0,4,3,3,1])

what happens when you do s.splice(-1, 1)?

look at your algorithm with this:
http://pythontutor.com/javascript.html#mode=display

i am using python tutor and it just shows all the numbers inside being spliced and then pushed to array. also its not -1. its splice(0,1) for every loop because after i do i--; it goes to next loop to increment.

ah, yeah , you are right. anyway, only one of these needs to be true for the condition to execute. s[0] !== s[-1] is what?

i just want to splice out the numbers that don’t have consecutive duplicates on the right or left really. so basically the take out the 1,0,4,1 and push them to arr. but i have no idea why my if condition isn’t working :confused:

you are using an OR logic operator. only one of the two conditions need to be true for the statement to execute
undefined (s[-1]) is different from a number, so this is true, and the statement is executed

yeah the OR operator works. either the condition is true then execute

so, what do you think is going wrong here?

undefined (s[-1)? what do u mean

you don’t have an item at idex -1, so s[-1] is undefined

oh so its undefined for everyone of them since its splice at 0 index every loop

Why are you splicing anyway, can you explain your logic here because this seems extremely overcomplicated.

well i just want to separate the unique consecutive values from the non-consecutives. like 4,4,4 and 3,3
edit: been here for over 6 hours so im still struggling :confused:

Why though? You don’t need to separate them out, you just need to add them up