-
Notifications
You must be signed in to change notification settings - Fork 335
Expand file tree
/
Copy path08 Bonfire Missing letters.js
More file actions
57 lines (43 loc) · 1.34 KB
/
08 Bonfire Missing letters.js
File metadata and controls
57 lines (43 loc) · 1.34 KB
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
/* Bonfire: Missing letters
Difficulty: 2/5
Find the missing letter in the passed letter range and return it.
If all letters are present in the range, return undefined.
Remember to use RSAP if you get stuck. Try to pair program. Write your own code.
Here are some helpful links:
String.charCodeAt()
String.fromCharCode()
Code by Rafael Rodriguez
rafase282@gmail.com
http://www.freecodecamp.com/rafase282
*/
function fearNotLetter(str) {
// Create our variables.
var firstCharacter = str.charCodeAt(0);
var valueToReturn = '';
var secondCharacter = '';
// Function to find the missing letters
var addCharacters = function(a, b) {
while (a - 1 > b) {
b++;
valueToReturn += String.fromCharCode(b);
}
return valueToReturn;
};
// Check if letters are missing in between.
for (var index = 1; index < str.length; index++) {
secondCharacter = str.charCodeAt(index);
// Check if the diference in code is greater than one.
if (secondCharacter - firstCharacter > 1) {
// Call function to missing letters
addCharacters(secondCharacter, firstCharacter);
}
// Switch positions
firstCharacter = str.charCodeAt(index);
}
// Check whether to return undefined or the missing letters.
if (valueToReturn === '')
return undefined;
else
return valueToReturn;
}
fearNotLetter('abce');