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.
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.
Input is an array of numbers
The array is not ordered
Output is an array of numbers with the sums of identical numbers.
Because the array is not ordered, you could sort it. That will then be the input to a loop.
You’re going to need to loop through the array, and you can just have a single loop for the whole function.
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.
Say you have an array like [1], output will be [1]. So now you loop over input array and push values to output array.
[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.
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.
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)
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])
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.
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
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
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