[Code Signal][js] alternating Sums

GY·2021년 9월 30일
0

알고리즘 문제 풀이

목록 보기
40/92
post-thumbnail

🎆문제

Given a rectangular matrix of characters, add a border of asterisks(*) to it.

Example

For

picture = ["abc",
"ded"]
the output should be

addBorder(picture) = ["*",
"abc",
"ded",
"*"]

🎇풀이

function addBorder(picture) {
    let original = picture;
    const starCount = original[0].length + 2;
    const star = "*";
    let matrix = new Array(starCount).fill(star).join('');

    original = original.map((str) => {return star + str + star})
    original.splice(0, 0, matrix)
    original.splice(original.length, 0, matrix)

    return original;
}

✨다른 풀이

function addBorder(picture) {
    var width = picture[0].length + 2;
    return [
        '*'.repeat(width),
        ...picture.map(line => `*${line}*`),
        '*'.repeat(width)
    ];
}

아...맞다. repeat을 쓰면 문자열로 반복이 가능했는데 잊어버렸다.
그리고 배열에 직접 써주어 바로 리턴해 코드가 훨씬 간결해졌다.

profile
Why?에서 시작해 How를 찾는 과정을 좋아합니다. 그 고민과 성장의 과정을 꾸준히 기록하고자 합니다.

0개의 댓글