CodeSignal Arcade – Add border

Problem

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

Example

For

picture = ["abc",
           "ded"]

the output should be

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

Solution

Idea

Another easy problem. I loop through the array and add * at the beginning and end of the element. After that, add the top and bottom border.

Code

function solution(picture) {
    for (let i = 0; i < picture.length; i++) {
        picture[i] = "*" + picture[i] + "*";
    }
    
    const pictureWidth = picture[0].length;
    
    return ["*".repeat(pictureWidth), ...picture, "*".repeat(pictureWidth)];
}
Leave a Reply 0

Your email address will not be published. Required fields are marked *