Search code examples
javascriptjqueryarraysangularjsbackbone.js

interchange array using shift method


I am trying to interchange array and print it using shift method but not sure whether I can use it or not.

Code Snippet below.

var points = [40, 100, 1, 5, 25, 10];

//trying to achieve like anotherPoints array
//var anotherPoints = [1, 5, 100, 40, 25, 10];

for (index = 0; index < points.length; index++) {
  points.shift();
  console.log(points);
}

Solution

  • Some logic to get the desired result:

    var points = [40, 100, 1, 5, 25, 10],
        temp1 = [], temp2 = [], anotherArray;
    points.forEach(function(val){
        if(val < 10 ) {
            temp1.push(val)
        } else {
            temp2.push(val);   
        }
    });
    anotherArray = temp1.sort().concat(temp2.sort(function(a,b){return b- a}));
    alert(anotherArray);

    It's not possible via shift or splice. Unless manually creating the array.