Border v2

Border v2

Given a rectangular matrix of characters, add a border of asteriks to it.

Example:
console.log(addBorder([“abc”, “ded”]));
[ ‘**’, ‘abc’ , ‘ded’, ‘**’]

function addBorder(picture) {
  const wall = '*'.repeat(picture[0].length + 2);

  picture.unshift(wall);
  picture.push(wall);
  
  for (let i = 1; i < picture.length -1;i++){
    picture[i] = '*'.concat(picture[i],'*')
  }
  return picture;

We create a function addBorder with parameter picture and two local variables wall const wall = ‘*‘.repeat(picture[0].length + 2);

const wall = ‘‘.repeat(picture[0].length + 2) means wall will be repeat ‘’ from picture[0] length + 2

picture.unshift(wall) picture.push(wall) We push a wall to the picture from both sides with unshift we add from beggining and with push to the end.

We create another for loop this one starts at 1 and i < picture.length -1 and i++ Inside the loop we do this operation picture[i] = ‘‘.concat(picture[i], ‘’)

In the end we return picture

comments powered by Disqus