My function 'extremeValue' takes 2 parameters, an array and a string "Maximum" or "Minimum", and depending on that string it returns the maximum or minimum value of the array. I used an example array 'values' to pass through the function and while it works out the minimum just fine, the maximum comes out to be the last value of the array. What's wrong with my code?
var values = [4, 3, 6, 12, 1, 3, 7];
function extremeValue(array, maxmin) {
if (maxmin === "Maximum") {
var max = array[0];
for (var i = 1; i < array.length; i++) {
if (array[i] > array[i-1]) {
max = array[i];
}
}
return max;
}
else if (maxmin === "Minimum") {
var min = array[0];
for (var j = 1; j < array.length; j++) {
if (array[j] < array[j-1]) {
min = array[j];
}
}
return min;
}
}
console.log(extremeValue(values, "Maximum"));
console.log(extremeValue(values, "Minimum"));