rotate.js

    0

    0

    The rotate function takes two arguments, nums and k. nums is an array of numbers, and k is the number of positions to rotate the array by. The rotate function loops through each number in the nums array, and assigns the corresponding number from arr to nums[i].

    
    var rotate = function(nums, k) {
        let arr = [];
        for (let i = 0; i < nums.length; i++) {
          let index = (i + k) % nums.length;
          arr[index] = nums[i];
        }
        for (let i = 0; i < nums.length; i++) {
          nums[i] = arr[i];
        }
      };
      
    Codiga Logo
    Codiga Hub
    • Rulesets
    • Playground
    • Snippets
    • Cookbooks
    soc-2 icon

    We are SOC-2 Compliance Certified

    G2 high performer medal

    Codiga – All rights reserved 2022.