samedi 27 juin 2015

Return null if any of the integers are negative or more than 10

I am trying to trying to return null if any of the integers are negative or more than 10. When I use this code, I get null, null, null. Instead of just null once. Expected: null, instead got: [null,null,null].

What could be the reason for this?

function upArray(arr) {
    for (var i = 0, len = arr.length; i < len; i++) {
        var num = arr[i];
        if (num > 9 || num <= 0) {
            var x = null;
            return x;
        }  
    }
    {
            var toNum = parseInt(arr.join('')) + 1;
            arr = toNum.toString().split('').map(Number);
            return arr;
        }
}

http://ift.tt/1GSTyA7

Aucun commentaire:

Enregistrer un commentaire