Created
March 21, 2018 20:46
-
-
Save nickihastings/f2d3b2d80fea511765a54a9674281b38 to your computer and use it in GitHub Desktop.
Find the missing letter in the passed letter range and return it. If all letters are present in the range, return undefined.
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
function fearNotLetter(str) { | |
//in unicode A=65 and Z=90, a=97 and z=122 | |
//get the starting letter's unicode, and then add one to check the next letter | |
var start = str.charCodeAt(0) + 1; | |
var missing = ''; //store missing letters | |
//loop over the string, get the letters unicode value and compare it to what the next letter's code should be: | |
for(var i = 1; i < str.length; i++){ | |
//if the codes don't match append the letter to the variable | |
if(str.charCodeAt(i) !== start){ | |
missing += String.fromCharCode(start); | |
start++; //add an extra one to the counter for the missing letter | |
} | |
start ++; | |
} | |
//if there's no missing letters return undefined. | |
if(missing == ''){ | |
missing = undefined; | |
} | |
return missing; | |
} | |
fearNotLetter("pqrt"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
function fearNotLetter(str) {
//Create array of alphabet
let alphabetArrray = "abcdefghijklmnopqrstuvwxyz".split("");
//Get the start and end index of the str from the alphabet
let startIndex = alphabetArrray.indexOf(str[0])
//startIndex = 18
let endIndex = alphabetArrray.indexOf(str[str.length-1])
//endIndex = 23
//Get the complete string range from the alphabet array
let strArray = alphabetArrray.slice(startIndex,endIndex)
//strArr = ["s","t","u","v","w","x"]
//Find the missing letter from the string array we created
let missingLetter = strArray.filter(item => (!str.includes(item)))
//missingLetter = ["u"]
//Check if missing letter has value
if( missingLetter.length > 0){
//If missing letter has a value return it as letter by using join method
return missingLetter.join("")
}else{
//This returns when there is no missing letter from the given string
return undefined
}
}
fearNotLetter("stvwx");