Javascript反向数组并减去2,然后减1,然后减0然后加1,加2等

时间:2016-03-29 01:42:38

标签: javascript

抱歉这个糟糕的头衔。我已经崩溃了足够的浏览器并寻求帮助。

以下是我要解决的具体问题。

创建一个名为'reversedLooper'的函数,当传递一个数组时,它将向后循环并从最后一个元素中减去2,从第二个元素到最后一个元素减去1,从第二个到最后一个减去0,从第四个到最后一个添加一个,添加2到第五个到最后,等等,直到它到达阵列的前面。完成后返回列表

4 个答案:

答案 0 :(得分:3)

使用[].map

很容易
myArray.map(function(item, index, arr) {
  return item + arr.length - index - 3;
});

答案 1 :(得分:1)

这是一个更简洁,更简洁的版本。

function reversedLooper(arr){
  var increaseBy = -2;
  for(var i = arr.length - 1; i >= 0; i--){
    arr[i] += increaseBy;
    increaseBy++
  }
  return arr;
}

答案 2 :(得分:0)

这真的很难看,但似乎满足了测试用例。

var array = [1,2,3,4,5,6,7,8,9,10,11,12,13];
function reversedLooper(array) {
    var newArray = [];
    var n = -3;
    for (var i = array.length - 1; i >= 0; i--) {
      n++;
        if (array[i] === array.length) {
            newArray.push(array[i] - 2);
        }
        else if (array[i] === array.length - 1) {
            newArray.push(array[i] - 1);
        }
        else if (array[i] === array.length - 2) {
            newArray.push(array[i]);
        }
        else {
            newArray.push(array[i] + n)
        }
    }
    return newArray;
}
console.log(reversedLooper(array));

答案 3 :(得分:0)

根据我的评论,我会使用新的小提琴插件来制作一些东西给你看。



let list = Immutable.List.of(1,2,3,4,5)

function createListOperationsMapping(list) {
  // Maybe consider dynamically creating a mapping of methods to indexes
  // If you return a Map from this that is something like:
  /*
   Map({
     0: (item) => item - 2,
     1: (item) => item - 1,
     // since there's a case where you subtract 0, so no change
     2: (item) => item,
     3: (item) => item + 1,
     4: (item) => item + 2,
     5: (item) => item + 3,
     // etc...
   })
   
   */
  // This would allow you to map the reversedList and based on the index you could just  
  // call the method from the map...
  // Like this (where the map from above ^^ is called listOperationMap)
  /*
  
    var mutatedReversedList = reversedList.map((item, index) => {
      return listOperationMap[index](item);
    })

   Notice I used the word "mutated" here. If you use Immutable, 
   you're actually returning a copy with updates rather than mutating, 
   but I wanted to make it clear that the result of mapping the 
   Immutable List will be a new Immutable List - not the mutated 
   previous list. Also, if you choose to work with Immutable, remember 
   you will need to set new variables for return values of updates to 
   the Immutable data structures.
  
   */
}

let reversedList = list.reverse() 

let reversedListSize = reversedList.size

let newList = reversedList.map((listItem, index) => {
  // Do stuff in here
})

console.log(list.toJS())
console.log(reversedList.toJS())
console.log(reversedListSize)

<script src="https://cdnjs.cloudflare.com/ajax/libs/immutable/3.7.6/immutable.min.js"></script>
&#13;
&#13;
&#13;

由于你的问题有点模糊,我基本上只是根据可以使用的内容抛出一些想法。希望这对于至少开始探索如何解决您要解决的问题非常有用。

无论哪种方式,我建议你看一下Immutable.js。

PS:如果你点击&#34;运行代码片段&#34;,然后破解你的开发工具,console.log将显示在你的控制台中。