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

integer empty variable javascript - Stack Overflow

programmeradmin2浏览0评论

I don't know why this return undefined?

var array = [6, 4, 6, 9];
var min;
var findminimum =   function(){
        array.reduce(function(prev, cur, index, array) {
                    if (cur < min) {
                        min = cur;
                    }
        }, 0);
        return min;
    };
console.log(findminimum());

but when i set:

var min = 20;

it is run ok, so anyone can explain me how to properly define num variable?

I don't know why this return undefined?

var array = [6, 4, 6, 9];
var min;
var findminimum =   function(){
        array.reduce(function(prev, cur, index, array) {
                    if (cur < min) {
                        min = cur;
                    }
        }, 0);
        return min;
    };
console.log(findminimum());

but when i set:

var min = 20;

it is run ok, so anyone can explain me how to properly define num variable?

Share Improve this question edited Apr 8, 2016 at 15:31 cнŝdk 32.2k7 gold badges60 silver badges80 bronze badges asked Apr 8, 2016 at 14:48 meksmeks 771 gold badge2 silver badges9 bronze badges 4
  • 3 if cur < min is never met, you never assign the min variable a value, meaning when it return min's, it returns null. Just initialize min to Number.MAX_SAFE_INTEGER – Redmega Commented Apr 8, 2016 at 14:52
  • 1 If min isn't set, cur < min will be paring a number to undefined which is false, min will never be set. One solution would be to additionally check if min is unset. Eg. if(cur < min || min === undefined) – amura.cxg Commented Apr 8, 2016 at 14:54
  • you are right, thank you :) – meks Commented Apr 8, 2016 at 14:56
  • Whats the point of reducing the array here if you are not paring prev with cur and not providing Number.MAX_SAFE_INTEGER for the initial value argument. – Redu Commented Apr 8, 2016 at 18:17
Add a ment  | 

4 Answers 4

Reset to default 1

In fact undefined in Javascript means that, this variable is not yet defined and that's exactly what's happening in your case:

You are using the variable min before assigning a value to it, so :

if (cur < min)

Will always return false, so there's no value assigned to min and min = cur; will never be reached, and your function will return undefined.

EDIT:

If you only write var min; you are just declaring the variable min in order to use it, you have to initialize it by assigning a value to it, in your case you should write:

var min = 0;

Now min will have the value 0, you can then pare it with other variables.

The problem is that because min is not initialized to a value it is undefined. Therefore your parison if (cur < min) is never true and min continues to be undefined. Also, if you're going to use reduce() for this instead of a simple loop, you may want to utilize it differently, here's a code sample that works:

var array = [6, 4, 6, 9, 1, -99];
var min = 9999;
var findminimum = function () {
    // you could return the final output of reduce()...
    return array.reduce(function (prev, cur, index, array) {
        if (cur < min) {
            min = cur;
        }

        return min; // ...then this return statement would be here inside the reduce callback
    }, 0);
};
console.log(findminimum());

You need to assign a value to min otherwise you won't be able to test its value

if (typeof min == "undefined" || cur < min) {
    min = cur;
}

Your main problem is, you use a variable from ouside of reduce min to store the minimum of the array. Array#reduce() is a method for getting a single result out of the iteration with a possible given start value.

Problems (order in reverse):

  • reduce(..., 0) start with 0. makes no sense, because it could be a minimum or a value which is too small, like in your array [6, 4, 6, 9].

  • better take no initialValue, because if no initialValue is given than reduce takes the first two elements for prev and cur. If the array has only one element, then this element is returned.

  • The short form of the required reduce:

    min = array.reduce(function (prev, cur) {
        return cur < prev ? cur : prev;
    });
    

Now you can put it into a function for the minimum and take an array as parameter:

function getMinimum(array) {
    return array.reduce(function (prev, cur) {
        return cur < prev ? cur : prev;
    });
}

document.write(getMinimum([6, 4, 6, 9]) + '<br>');
document.write(getMinimum([6, 4]) + '<br>');
document.write(getMinimum([42]) + '<br>');

发布评论

评论列表(0)

  1. 暂无评论