For loop not finding max value of array (Javascript) - Stack Overflow

My function 'extremeValue' takes 2 parameters, an array and a string "Maximum" or &

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 es 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"));

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 es 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"));

Share asked May 28, 2017 at 3:40 KimahsoKimahso 231 silver badge4 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 4

Change the checking with maximum value.

if (maxmin === "Maximum") {
    var max = array[0];
    for (var i = 1; i < array.length; i++) {
        if (array[i] > max) { //not just before 'array[i-1]'
            max = array[i];
        }
    }
    return max;
}

OR

Simply Use

Math.max.apply( Math, values );

to find Maximum from values.and

Math.min.apply( Math, values );

for minimum values.

In each loop you must pare an item with maximum (or minimum) value, not before item:

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] > max) {
                max = array[i];
            }
        }
        return max;
    }
    else if (maxmin === "Minimum") {
        var min = array[0];
        for (var j = 1; j < array.length; j++) {
            if (array[j] < min) {
                min = array[j];
            }
        }
        return min;
    }
}

console.log(extremeValue(values, "Maximum")); 
console.log(extremeValue(values, "Minimum"));

Alternatively you can use the Math function to shorten your code:

var values = [4, 3, 6, 12, 1, 3, 7];
            
function extremeValue(array, maxmin) {
	if (maxmin === "Maximum") {
           return Math.max.apply(null, array);
	}
	else if (maxmin === "Minimum") {
	   return Math.min.apply(null, array);
	}
}
console.log(extremeValue(values, "Maximum")); 
console.log(extremeValue(values, "Minimum"));

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745046092a4608106.html

相关推荐

  • For loop not finding max value of array (Javascript) - Stack Overflow

    My function 'extremeValue' takes 2 parameters, an array and a string "Maximum" or &

    8小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信