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];
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);
+ 2
Thank you