Skip to content

Instantly share code, notes, and snippets.

@nickihastings
Created March 21, 2018 20:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nickihastings/f2d3b2d80fea511765a54a9674281b38 to your computer and use it in GitHub Desktop.
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.
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");
@Ngarison
Copy link

function fearNotLetter(str) {
let alphabet="abcdefghijklmnopqrstuvwxyz".split("");
let debut=0;
let fin=0;
let missingEl="";
for(let i=0;i<alphabet.length;i++){
if(str[0]==alphabet[i]){
debut=i;
console.log(debut);
}
if(str[str.length-1]==alphabet[i]){
fin=i;
console.log(fin);
}
}

for(let i=debut;i<=fin;i++){
if(str.indexOf(alphabet[i])==-1){
missingEl=alphabet[i];
console.log(missingEl);
}
}

if(str.length==alphabet.length){
return undefined;
}

return missingEl;
}

fearNotLetter("abce");

@Jfnrosimo
Copy link

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");

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment