-
Notifications
You must be signed in to change notification settings - Fork 63
/
Copy pathletter_changes.js
109 lines (94 loc) · 2.65 KB
/
letter_changes.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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/**
* Have the function letterChanges(str) take the str parameter being passed and
* modify it using the following algorithm. Replace every letter in the string
* with the letter following it in the alphabet (ie. c becomes d, z becomes a).
* Then capitalize every vowel in this new string (a, e, i, o, u) and finally
* return this modified string.
* @param {string} str
* @return {string}
*/
function letterChanges(str) {
let newString;
newString = caesarCipher(str, 1);
newString = capitalizeVowels(newString);
return newString;
}
/*
* Helper functions below
*/
/**
* Returns a new string with a Caesar cipher applied
* https://en.wikipedia.org/wiki/Caesar_cipher
* @param {string} str
* @param {number} key
* @return {string}
*/
function caesarCipher(str, key) {
const newString = str
.split('')
.map(char => (isAlpha(char) ? shiftAlphaChar(char, key) : char))
.join('');
/**
* Shifts a character by a delta value
* @param {string} char Assumed to be an alphabetic character
* @param {number} delta
* @return {string}
*/
function shiftAlphaChar(char, delta) {
const charCode = char.charCodeAt(0);
const alphabetStart = isUpperCase(char)
? 'A'.charCodeAt(0)
: 'a'.charCodeAt(0);
const distanceFromA = charCode - alphabetStart;
const newDistanceFromA = (distanceFromA + delta) % 26;
const newCharCode = newDistanceFromA + alphabetStart;
return String.fromCharCode(newCharCode);
}
return newString;
}
/**
* Checks if a string only includes alphabetic characters
* @param {string} str
* @return {Boolean}
*/
function isAlpha(str) {
const alphabet = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
return str.split('').every(char => alphabet.includes(char));
}
/**
* Checks if a string does not contain any lowercase characters
* @param {string} str
* @return {Boolean}
*/
function isUpperCase(str) {
return str === str.toUpperCase();
}
/**
* Checks if a string does not contain any uppercase characters
* @param {string} str
* @return {Boolean}
*/
function isLowerCase(str) {
return str === str.toLowerCase();
}
/**
* Capitalizes the vowels in a string and returns a new string
* @param {string} str
* @return {string}
*/
function capitalizeVowels(str) {
const vowels = 'aeiou';
const newString = str
.split('')
.map(char => (vowels.includes(char) ? char.toUpperCase() : char))
.join('');
return newString;
}
module.exports = {
letterChanges,
caesarCipher,
isAlpha,
isUpperCase,
isLowerCase,
capitalizeVowels
};