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

008 Alphabetic Shift

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

// Given a string, replace each its character by the next one in the English alphabet (z would be replaced by a).

// Example

// For inputString = "crazy", the output should be alphabeticShift(inputString) = "dsbaz".

function alphabeticShift(values) {
  const alphabets = 'abcdefghijklmnopqrstuvwxyz'.split('')
  values = values.split('').map(a => {
    const index = alphabets.indexOf(a)
    return a !== 'z' ? alphabets[index + 1] : alphabets[0]
  })
  return values.join('')
}

test('alphabetic Shift', () => {
  expect(alphabeticShift('crazy')).toEqual('dsbaz')
})

Created 2019-12-07T17:23:15+05:18 · Edit