How to generate sequence of numbers/chars in javascript?

前端 未结 18 1191
被撕碎了的回忆
被撕碎了的回忆 2020-12-13 06:22

Is there a way to generate sequence of characters or numbers in javascript?

For example, I want to create array that contains eight 1s. I can do it with for loop, bu

18条回答
  •  Happy的楠姐
    2020-12-13 06:53

    If like me you use linspace a lot, you can modify your version of linspace easily like so:

    function linSeq(x0, xN) {
        return linspace(x0, xN, Math.abs(xN-x0)+1);
    }
    
    function linspace(x0, xN, n){
    
        dx = (xN - x0)/(n-1);
        var x = [];
        for(var i =0; i < n; ++i){
            x.push(x0 + i*dx);
        }
    
        return x;
    }
    

    You can then use linSeq in any direction, e.g. linSeq(2,4) generates 2,3,4 while linSeq(4,2) generates 4,3,2.

提交回复
热议问题