I have done the below exercise from Codility: https://app.codility.com/programmers/lessons/2-arrays/cyclic_rotation/
Below are my answers:
function solution(A, K) {
for (let i = 0; i = K; i++) {
A.shift(A.pop());
}
return A;
}
Can anyone comment on my answer?
Also I don’t understnad the official answer as below.
What does " var newPos = (i+K) % A.length;" mean? What is “newPos” and “%” in this context?
I wonder if I should continue doing Codility exercises.
function solution(A, K) {
// write your code in JavaScript (Node.js 4.0.0)
var result = [];
if(A.length === 1 || K === 0) {
return A;
}
for(var i=0; i<A.length; i++) {
var newPos = (i+K) % A.length;
result[newPos] = A[i];
}
return result;
}