Border

Border

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 lengthOfWall = picture[0].length  + 2;
  let wall = '';

  for(let i = 0; i< lengthOfWall; i++) {
    wall = wall.concat('*')
  }
  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 lenghtOfWall const lengthOfWall = picture[0].length + 2;

To add the two indexex to the length. then we have for loop where we loop from 0 until lenghtOfWall i++ Inside the for loop we use do this wall = wall.concat(‘’)

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