rotateWords

samuel Jo·2023년 4월 12일
0

codewars

목록 보기
19/46
post-thumbnail

Descriptions

Create a function named rotate() that accepts a string argument and returns an array of strings with each letter from the input string being rotated to the end.

Note: The original string should be included in the output array The order matters. Each element of the output array should be the rotated version of the previous element. The output array SHOULD be the same length as the input string The function should return an emptry array with a 0 length string, '', as input

Ex)
rotate("Hello") // => ["elloH", "lloHe", "loHel", "oHell", "Hello"]

function rotate(string) {
    if (string === "") {
        return [];
    }
    let res = [];
    let str = string;
    for (let i = 0; i < str.length; i++) {
        str = str.slice(1) + str[0];
        res.push(str);

    }
    return res;
}

console.log(rotate("Hello")); 
profile
step by step

0개의 댓글