Commit 0cb094b4 authored by flothoni's avatar flothoni
Browse files

tools; add a function to remove position from array and decrease upper values

Link to #4541
parent 8f4e88db
......@@ -801,3 +801,18 @@ function removeDuplicate(array) {
})
return clean_array
}
/**
* Filter value given of an array and decrease greater values
*/
function removeEltAndDecrease(array, value) {
for (var i = 0; i < array.length; i++) {
if (array[i] > value) {
array[i] = array[i] - 1
} else if (array[i] == value) {
array.splice(i, 1)
i = i-1
}
}
return array
}
......@@ -436,3 +436,9 @@ QUnit.test("remove duplicates", function(assert) {
var array = [0,1,2,3,3,3,4,0]
assert.deepEqual(removeDuplicate(array), [0,1,2,3,4], "correct array after removeDuplicate (4)")
});
QUnit.test("remove elt in decrease", function(assert) {
var listel = [0,1,2,4,5,7,8]
assert.deepEqual(removeEltAndDecrease(listel, 3), [0,1,2,3,4,6,7], "correct array after removeEltAndDecrease (3)")
});
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment