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

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:35:15+05:30 · Edit