Swapping pairs in arrays recursively - Javascript Array Operation

Javascript examples for Array Operation:Associative Array

Description

Swapping pairs in arrays recursively

Demo Code

ResultView the demo in separate window

<html>
   <head> 
      <meta name="viewport" content="width=device-width, initial-scale=1"> 
      <script type="text/javascript">
    window.onload=function(){/*from  w  w  w. j  a  v a  2 s.co  m*/
var pairSwapRecursive = function(arr) {
    if (arr.length < 2) {
        return arr;
    }
    return [arr[1], arr[0]].concat(pairSwapRecursive(arr.slice(2)));
};
console.log(pairSwapRecursive([1,2,3,4,5]));
    }

      </script> 
   </head> 
   <body>  
   </body>
</html>

Related Tutorials