code.ashish.me

Atom feed

Recently added: 128 Longest Consecutive Sequence, 347 Top K Frequent Elements, 045 Jump Game 2, 228 Summary Ranges, 219 Contains Duplicate 2

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:47:16+05:30 · Edit