I am writing a code which takes an input string and an array of markers and removes everything from the markers to the end of the on which it is found. I have a function whi
You need to work backwards through the positions
array, because when you splice out some elements of arr
, the indexes of everything after those elements change.
function solution(input, markers){
var positions = identify(input, markers);
var arr = input.split('');
for(var i = positions.length-2; i >= 0; i-=2){
arr.splice(positions[i], positions[i+1] - positions[i]);
console.log(positions[i+1]);
}
return arr.join('');
}