最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin2浏览0评论

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

发布评论

评论列表(0)

  1. 暂无评论