I am trying to manipulate an array. For each operation, I need to add a value to each the array element between two given indices, inclusive. Here is one example:
0 1 100 // From index 0 to 1, add 100
1 4 100 // From index 1 to 4, add 100
2 3 100 // From index 2 to 3, add 100
// Expected Result:
[100, 200, 200, 200, 100]
// Explanation:
[100, 100] // After the first update.
[100, 200, 100, 100, 100] // After the second update.
[100, 200, 200, 200, 100] // After the third update.
And this was as far as I got:
function arrayManipulation(n, queries) {
let newArr = [];
for (let i = 0; i < queries.length; i++) {
let indexIni = queries[i][0];
let indexEnd = queries[i][1];
let indexSum = queries[i][2];
for (indexIni; indexIni < indexEnd; indexIni++) {
console.log(indexIni, indexEnd, indexSum);
newArr.splice(indexIni, 0, indexSum);
}
}
console.log(newArr);
}
let n1 = 5;
let queries1 = [
[0, 1, 100],
[1, 4, 100],
[2, 3, 100]
];
arrayManipulation(n1, queries1);
What I was trying to do was work on top of the second parameter of splice() so that I could somehow add it up to the number I was going to input.
The way I'm trying, is it possible? Or is there a simpler method?