{"id":307,"date":"2020-02-17T15:12:00","date_gmt":"2020-02-17T15:12:00","guid":{"rendered":"https:\/\/danwritescode.com\/?p=307"},"modified":"2020-06-22T13:33:13","modified_gmt":"2020-06-22T13:33:13","slug":"codility-lesson-2-100-javascript-solution-cyclic","status":"publish","type":"post","link":"https:\/\/danwritescode.com\/codility-lesson-2-100-javascript-solution-cyclic\/","title":{"rendered":"Codility Cyclic Rotation – 100% Correct Solution"},"content":{"rendered":"\n

This is a Javascript implementation for the Codility Cyclic Rotation in Lesson 2 – Arrays.<\/p>\n\n\n\n

The challenge is to rotate an array A a number of K times.<\/p>\n\n\n\n

The solution is quite trivial, we just need to split the array in 2 at the K lenght-K index and then add the two array. There should be a test if K is larger then the length though, and it case it is, it should be reduced with the % operator.<\/p>\n\n\n\n

Here is the 100% Javascript solution. See it in action here<\/a>.<\/p>\n\n\n\n

\nfunction solution (A,K) {\n    if (K >= A.length) { K = K % A.length;}\n    if (K == 0) return A;\n    else {\n        let B = A.slice(A.length-K, A.length);\n        let C = A.slice(0, A.length-K);\n        D = B.concat(C);\n        return (D);\n    }\n}\n\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"

This is a Javascript implementation for the Codility Cyclic Rotation in Lesson 2 – Arrays. The challenge is to rotate an array A a number of K times. The solution is quite trivial, we just need to split the array in 2 at the K lenght-K index and then add the two array. There should be a test if K is larger then the length though, and it case it is, it should be reduced with the % operator. Here is the 100% Javascript solution. See it in action here.<\/p>\n","protected":false},"author":1,"featured_media":334,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/danwritescode.com\/wp-json\/wp\/v2\/posts\/307"}],"collection":[{"href":"https:\/\/danwritescode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/danwritescode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/danwritescode.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/danwritescode.com\/wp-json\/wp\/v2\/comments?post=307"}],"version-history":[{"count":0,"href":"https:\/\/danwritescode.com\/wp-json\/wp\/v2\/posts\/307\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/danwritescode.com\/wp-json\/wp\/v2\/media\/334"}],"wp:attachment":[{"href":"https:\/\/danwritescode.com\/wp-json\/wp\/v2\/media?parent=307"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/danwritescode.com\/wp-json\/wp\/v2\/categories?post=307"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/danwritescode.com\/wp-json\/wp\/v2\/tags?post=307"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}