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

javascript - Doubling an array? - Stack Overflow

programmeradmin7浏览0评论

I'm trying to figure out how to get my array

var arr = [1,2,3,4];

to double. I've made a function with a for loop to print each number twice but for some reason "4" gets printed three times rather than two. I've tried to arr.pop it but nothing happens. Am I putting it in the wrong place or do I have to do something else? My code:

function start()
{
    var arr = [1,2,3,4];
    var doubled = doubleList(arr);
    println(doubled);
}

function doubleList(arr)
{
    for(var i = 0; i < arr.length; i++)
    {
        var cur = arr[i];
        print(cur);
        print(",");
        print(cur);
        print(",");
    }
    return cur;
}

And my results:

1,1,2,2,3,3,4,4,4

I'm trying to figure out how to get my array

var arr = [1,2,3,4];

to double. I've made a function with a for loop to print each number twice but for some reason "4" gets printed three times rather than two. I've tried to arr.pop it but nothing happens. Am I putting it in the wrong place or do I have to do something else? My code:

function start()
{
    var arr = [1,2,3,4];
    var doubled = doubleList(arr);
    println(doubled);
}

function doubleList(arr)
{
    for(var i = 0; i < arr.length; i++)
    {
        var cur = arr[i];
        print(cur);
        print(",");
        print(cur);
        print(",");
    }
    return cur;
}

And my results:

1,1,2,2,3,3,4,4,4
Share Improve this question asked Feb 14, 2018 at 16:40 AlyssaAlyssa 252 gold badges3 silver badges8 bronze badges 8
  • 3 You're using print? I didn't know JS had a print for that purpose. And are you sure the last 4 isn't the return value of the function? – Carcigenicate Commented Feb 14, 2018 at 16:42
  • 6 Your function already does the printing - no need to do another print after the function println(doubled);. That will print the last element again as the last element is what is returned from your function. As a side note, this does not look like valid js. – takendarkk Commented Feb 14, 2018 at 16:42
  • 1 Coz u return cur and then print it.. – StyleSh1t Commented Feb 14, 2018 at 16:43
  • 2 @DBQ That is incorrect. – Code-Apprentice Commented Feb 14, 2018 at 16:44
  • 1 Note that a function should typically print or return a value, but not both. – Code-Apprentice Commented Feb 14, 2018 at 16:46
 |  Show 3 more ments

4 Answers 4

Reset to default 4

You are returning the last read character in doubleList(), and then printing it in start(). Thats why you have last character 3 times instead of 2.

function start()
{
    var arr = [1,2,3,4];
    var doubled = doubleList(arr);
    println(doubled); // here is the problem. try removing this
}

function doubleList(arr)
{
    for(var i = 0; i < arr.length; i++)
    {
        var cur = arr[i];
        print(cur);
        print(",");
        print(cur);
        print(",");
    }
    return cur;
}

The last 4 that is printed es from

println(doubled);

this is maintained in the same line as the others due to the other print statements being simple print and not println

My doubleList function would look like this:

function doubleList(arr) {
    var result = [];
    for (var i in arr) {
        result.push(arr[i]);
        result.push(arr[i]);
    }
    return result
}

Sorry I missunderstood the question, here is an elegant ES6 solution.

const arr = [1,2,3,4];

const double2 = arr.reduce( (res, current, index, array) => {
    return res.concat([current, current]);
}, []);

console.log(double2)

发布评论

评论列表(0)

  1. 暂无评论