-
Notifications
You must be signed in to change notification settings - Fork 0
/
add_letters.js
33 lines (27 loc) · 909 Bytes
/
add_letters.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
// 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'
function addLetters(...letters) {
if (letters.length == 0) {
return 'z'
}
const alphabet = [...'abcdefghijklmnopqrstuvwxyz']
let total_count = 0
letters.forEach((letter) => {
total_count += alphabet.indexOf(letter) + 1
})
while (total_count > 26) {
total_count = total_count - 26
}
return alphabet[total_count - 1]
}