I am trying to write a function that takes in an array and a max cutoff number. The number is the max number that the string can contain, and this number replaces all array elements greater than the number. For example, if the array is [1,2,3,4,5,6] and the cutoff number is 4, the output should be [1,2,3,4,4,4].
This is what I have so far, but I am only getting [1,2,3,4,5] as the output instead of [1,2,3,3,3]. Any suggestions??
var maxCutoff = function(array, number) {
for (var i=0; i<array.length; i++) {
if (array.indexOf(i) > number) {
array.pop();
array.push(number);
return array;
} else if (array.indexOf(i) <= number) {
return array;
}
}
}
console.log(maxCutoff([1,2,3,4,5], 3));