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

javascript - recursion in array to find odd numbers and push to new variable - Stack Overflow

programmeradmin0浏览0评论

I tried to recursion those arrays to find odd/even numbers then push them to newArr but the result, not an array, that result is the string with numbers the result after found the odd/even numbers.

this is the code i wrote,

function odd(nums){
  var result = [];
  if(nums.length === 0) {
    return result;
  } else  if (nums[0] % 2 === 0){
    result.push(nums[0])
    // return odd(nums.slice(1))
  };
  return result + odd(nums.slice(1));
};

var arr = [1,8,3,4,4,5,9,13,13,9,10];
var print = odd(arr);
console.log(print)

I tried to recursion those arrays to find odd/even numbers then push them to newArr but the result, not an array, that result is the string with numbers the result after found the odd/even numbers.

this is the code i wrote,

function odd(nums){
  var result = [];
  if(nums.length === 0) {
    return result;
  } else  if (nums[0] % 2 === 0){
    result.push(nums[0])
    // return odd(nums.slice(1))
  };
  return result + odd(nums.slice(1));
};

var arr = [1,8,3,4,4,5,9,13,13,9,10];
var print = odd(arr);
console.log(print)
if i don't write return result + odd(nums.slice(1)); the result nothing / undefined,

can anyone help me to explain why that result in a string, not an array I wanted

Share Improve this question asked Jan 8, 2019 at 10:07 Zum DummiZum Dummi 2331 silver badge12 bronze badges
Add a ment  | 

5 Answers 5

Reset to default 4

You need to concat arrays. + does only work for strings or for numbers.

BTW, after block statements { ... }, you do not need a semicolon.

function odd(nums){
    var result = [];
    if (nums.length === 0) {
        return result;
    } 
    if (nums[0] % 2 === 0) {
        result.push(nums[0]);
    }
    return result.concat(odd(nums.slice(1)));
}

var arr = [1, 8, 3, 4, 4, 5, 9, 13, 13, 9, 10];
var print = odd(arr);
console.log(print);

Recursion is probably a bad idea in JS, you should use it unless there is no better way to solve a problem. It will be slower and possibly result in overflow, if too many calls of recursive function is made.

To get the result you needed, it is better to use array filter method. It is clearer and faster.

var arr = [1,8,3,4,4,5,9,13,13,9,10];
var odd = arr.filter(n => !(n % 2))
console.log(odd)

As for the original question, I'd do it this way:

function odd(nums, result = []){
  if (!nums.length) return result; // no need for else, this way clearer
  if (nums[0] % 2 === 0) result.push(nums[0])
  return odd(nums.slice(1), result)
};

var arr = [1,8,3,4,4,5,9,13,13,9,10];
console.log(odd(arr))


!!! Caution, next snippet WILL result in StackOverflow !!!

You may decrease array length to 10000 to check that it will work just fine.

function odd(nums, result = []){
  if (!nums.length) return result; // no need for else, this way clearer
  if (nums[0] % 2 === 0) result.push(nums[0])
  return odd(nums.slice(1), result)
};

const arr = Array.from({length: 100000}, (_,i) => i)
const result = odd(arr)
console.log(`Array length: ${arr.length}`)
console.log(`Result length: ${result.length}`)
console.log(result.slice(0,5))

You could use concat method.

function odd(nums){
  var result = [];
  if(nums.length === 0) {
    return result;
  } else  if (nums[0] % 2 === 0){
    result.push(nums[0])
    // return odd(nums.slice(1))
  }
  return result.concat(odd(nums.slice(1)));
}

var arr = [1,8,3,4,4,5,9,13,13,9,10];
var print = odd(arr);
console.log(print)

Another approach would be to pass the array containing the result as parameter, since it will be a reference, you don't have to return anything.

function odd(nums, result)
{
    if(nums.length != 0)
    {
        if (nums[0] % 2 === 0)
        {
            result.push(nums[0]);
        }
        odd(nums.slice(1), result);
    }
}

var arr = [1,8,3,4,4,5,9,13,13,9,10];
var result = [];
odd(arr, result);
console.log(result)

Instead of the concat you need in place of the string concatenation + you use now, better use filter

var odd = [], 
   even = [1,8,3,4,4,5,9,13,13,9,10]
  .filter(function(x) { if (x % 2 === 0) return true; else odd.push(x) } );
console.log(odd,even)

发布评论

评论列表(0)

  1. 暂无评论