code.ashish.me

Atom feed

Recently added: 02 Count Of Subset Sum, 416 Partition Equal Subset Sum, 01 Subset Sum, 518 Coin Change 2, 983 Minimum Cost For Tickets

003 Add Border

/**
 *
 * Ashish Patel
 * e: ashishsushilPatel@gmail.com
 * w: https://ashish.me
 *
 */

/* 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(values) {
  const wall = '*'.repeat(values[0].length+2)
  values = values.map((str) => `*${str}*`)
  values.push(wall)
  values.unshift(wall)
  return values
}

test('add Border', () => {
  expect(addBorder(['abc', 'ded'])).toEqual(['*****', '*abc*', '*ded*', '*****'])
})

Created 2019-12-05T02:35:16+05:18 · Edit