0

create a function to change array position in JavaScript that last element comes first?

let Array1 = function (nums, k) { for (let i = 0; i < k; i++) { nums.unshift(nums.pop()); } return nums; }; var nums = [1, 2, 3, 4, 5]; var k = nums.length; console.log(Array1); i didn't get the out put [5,1,2,3,4];

29th Apr 2022, 2:29 PM
TINKLE DASH
TINKLE DASH - avatar
2 odpowiedzi
+ 3
TINKLE DASH Don't use for loop And also Array1 is a function so you should pass array nums and length to get new array. ---------- let Array1 = function (nums, k) { //for (let i = 0; i < k; i++) { nums.unshift(nums.pop()); //} return nums; }; var nums = [1, 2, 3, 4, 5]; var k = nums.length; nums = Array1(nums, k) console.log(nums);
29th Apr 2022, 2:55 PM
A͢J
A͢J - avatar
+ 2
Thank you
29th Apr 2022, 2:59 PM
TINKLE DASH
TINKLE DASH - avatar