0

Right now this function console.logs .1 .2 .3 .4 .5 .6 .7 .8 .9 1

I am looking for a way to reverse this, so it logs 1 .9 .8. .7 .6 .5 .4 .3 .2 .1 instead. You can see my attempt, which is commented.

function blah(size) {
    for (var x = 1; x <= size; x++) {
        var alpha = (1 / size) * x;
        //var alpha = (1 / x) * (1 / size)
        console.log(alpha);
    }
}

blah(10);
4

2 回答 2

2

您可以反转循环

for(var x = size; x >= 1; x--) {

或更改您的公式

var alpha = 1 - ((x - 1) / size);

0我认为循环从 开始并具有 index是最惯用的i,所以我可能会这样做:

for(var i = 0; i < size; i++) {
  var alpha = 1 - (i / size);
}
于 2013-07-05T16:52:44.320 回答
0

只需反转循环的方向:

for (var x = size; x >= 1; x--) {
于 2013-07-05T16:52:10.000 回答