Skip to content

Latest commit

 

History

History
49 lines (37 loc) · 1.31 KB

alphabetical-addition.md

File metadata and controls

49 lines (37 loc) · 1.31 KB

Alphabetical Addition 7 Kyu

LINK TO THE KATA - ALGORITHMS

Description

Your task is to add up letters to one letter.

The function will be given a variable amount of arguments, each one being a letter to add.

Notes:

  • Letters will always be lowercase.
  • Letters can overflow (see second to last example of the description)
  • If no letters are given, the function should return 'z'

Examples:

addLetters('a', 'b', 'c') = 'f'
addLetters('a', 'b') = 'c'
addLetters('z') = 'z'
addLetters('z', 'a') = 'a'
addLetters('y', 'c', 'b') = 'd' // notice the letters overflowing
addLetters() = 'z'

Solution

const addLetters = (...letters) => {
  // console.log('a'.charCodeAt(0) - 96) --> 1
  // console.log(String.fromCharCode(1 + 96)) --> 'a'
  // ...
  // console.log('z'.charCodeAt(0) - 96) --> 26
  // console.log(String.fromCharCode(26 + 96)) --> 'z'

  const sum = letters.reduce((accumulator, currentValue) => {
    const newAccumulator = accumulator + currentValue.charCodeAt(0) - 96
    return newAccumulator > 26 ? newAccumulator - 26 : newAccumulator
  }, 0)

  return letters.length === 0 ? 'z' : String.fromCharCode(sum + 96)
}