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

javascript - How to create an array of a given value recursively? - Stack Overflow

programmeradmin0浏览0评论

I'm trying to work on recursion with javascript, and I'm having trouble figuring out how to do this recursively. For example, fillArray(num, len) with num = 5, and len = 3, would return [5,5,5]. I'm honestly stuck, and I haven't been able to make progress on this. It's not much, but this is what I have. Could someone please help?

var fillArray = function(num, len) {
    var list = [];
    if (length === 0) {
        return [];
    } 
    return list.concat(function(value, length--));
}

I'm trying to work on recursion with javascript, and I'm having trouble figuring out how to do this recursively. For example, fillArray(num, len) with num = 5, and len = 3, would return [5,5,5]. I'm honestly stuck, and I haven't been able to make progress on this. It's not much, but this is what I have. Could someone please help?

var fillArray = function(num, len) {
    var list = [];
    if (length === 0) {
        return [];
    } 
    return list.concat(function(value, length--));
}
Share Improve this question asked Apr 4, 2016 at 21:46 TheRealFakeNewsTheRealFakeNews 8,25324 gold badges77 silver badges132 bronze badges 5
  • 2 why would you want to use recursion for this? that's a bad idea. – dandavis Commented Apr 4, 2016 at 21:48
  • 2 Javascript has builtin functionality for this: Array(3).fill(5). You can just use that and if you really must use recursion you can just recursively do nothing a few times. – Paul Commented Apr 4, 2016 at 21:49
  • there are some errors that I see, you need to at least have fillArray(val....) not just function, and length is not len – omarjmh Commented Apr 4, 2016 at 21:49
  • 1 he doesnt want Array.fill, hes learning recursion – omarjmh Commented Apr 4, 2016 at 21:50
  • This looks like a homework assignment. You are actually pretty close. You just need to add your array to the signature so you can add the num to it. When you are done just return that array. – Gremash Commented Apr 4, 2016 at 21:52
Add a ment  | 

5 Answers 5

Reset to default 4

This can be done easier iteratively

var fillArray = function(num, len) {
   var result = [];

   for (var i = 0; i < len; i++) {
      result.push(num);
   }

   return result;
}

If you really must use recursion this should work

var fillArray = function(num, len) {
   if (len === 0) {
      return [];
   }

   return [num].concat(fillArray(num, len - 1));
}

You can do it like this, but it's not very efficient to be honest.

function arrayFill(num, length){
  if(length === 0) return [];
  return [].concat(num, arrayFill(num, length - 1));
}

console.log(arrayFill(5, 3));

Something like this:

    var fillArray = function(num, len, list) {
        list.push(num);
        if (list.length === len) {
            return list;
        } else {
            return fillArray(num, len, list);
        }
    }

    var num = 3;
    var len = 3;
    var list = [];

    console.log(fillArray(num, len, list));
var fillArray = function(num, len) {
   var result = [];

   if (len > 1) result.push(fillArray(num, len - 1));
   result.push(num);

   return result;
}

x = fillArray(5, 3);

An example using recursion:

function fillArray( num, length) {
    var array;
    if( length) {
        array = fillArray( num, length-1);
        array.push( num);
        return array;
    }
    else {
       return [];
    }
}

The recursive function calls itself with length decremented by 1 until length is zero. Then it returns an empty array. In the return path, at each level of decreasing recursion, the value of number gets pushed onto the array.

Recursion generally requires a condition to stop further recursion (here by length - 1 reaching zero), and returning the value of the recursive call ( here the value of array).

发布评论

评论列表(0)

  1. 暂无评论