-
-
Save nirajkrz/1a6bc501f282d067f79f to your computer and use it in GitHub Desktop.
http://www.freecodecamp.com/nirajkrz 's solution for Bonfire: Missing letters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Bonfire: Missing letters | |
// Author: @nirajkrz | |
// Challenge: http://www.freecodecamp.com/challenges/bonfire-missing-letters | |
// Learn to Code at Free Code Camp (www.freecodecamp.com) | |
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"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment