I’m trying to do this coding challenge that takes a list and an index and returns the values in increments of the index. for example…
combo([1, 2, 3, 4], 2) ➞ [[1, 2], [1, 3], [1, 4], [2, 3], [2, 4], [3, 4]]
when I try to grab the values it also grabs the commas and messes up the code. It’s currently returning 1,23,4
and so on.
let newList = [];
function nCombo(list, index) {
let newIndex = 0 + index;
for (i = 0; list.length >= i; i += index) {
newList.join(list.slice(i, newIndex));
console.log(`slice = ${list.slice(i,newIndex)}`)
console.log(newList);
newIndex += index;
}
console.log(newList);
}
nCombo([1, 2, 3, 4, 5, 6], 2)
I tried to use join before I was using newList += list.slice(i , newIndex);
I don’t know why it’s doing this
2
Answers
This was my attempt at the coding challenge you suggested. I’m not sure if my for loops provide the correct functionality, but
.push()
is working just fine.Hopefully this helps,
You could take a recursive approach.