I am trying to solve the following issue:
Find the missing letter in the passed letter range and return it. If all letters are present in the range, return undefine
Note that you have a typo in alphabet
: There are two "e"s.
You could split the string into an array, then use the some method to short-circuit the loop when you don't find a match:
function fearNotLetter(str) {
var alphabet = 'abcdefghijklmnopqrstuvwxyz',
missing,
i= 0;
str.split('').some(function(l1) {
var l2= alphabet.substr(i++, 1);
if(l1 !== l2) {
if(i===1) missing= undefined;
else missing= l2;
return true;
}
});
return missing;
}
console.log(fearNotLetter('abce')); //d
console.log(fearNotLetter('bcd')); //undefined
console.log(fearNotLetter('abcdefghjklmno')); //i
console.log(fearNotLetter('yz')); //undefined
function fearNotLetter(str) {
//transform the string to an array of characters
str = str.split('');
//generate an array of letters from a to z
var lettersArr = genCharArray('a', 'z');
//transform the array of letters to string
lettersArr = lettersArr.join('');
//substr the a to z string starting from the first letter of the provided
string
lettersArr = lettersArr.substr(lettersArr.indexOf(str[0]), str.length);
//transform it again to an array of letters
lettersArr = lettersArr.split('');
//compare the provided str to the array of letters
for(var i=0; i<lettersArr.length;i++){
if(str[i] !== lettersArr[i])
return lettersArr[i];
}
return undefined;
}
function genCharArray(charA, charZ) {
var a = [], i = charA.charCodeAt(0), j = charZ.charCodeAt(0);
for (; i <= j; ++i) {
a.push(String.fromCharCode(i));
}
return a;
}
fearNotLetter("bcd");
This will do what you're looking for:
Hit run and check your console
function missingLetter (str) {
var alphabet = ("abcdefghijklmnopqrstuvwxyz");
var first = alphabet.indexOf(str[0]);
var strIndex = 0;
var missing;
for (var i = first ; i < str.length ; i++) {
if (str[strIndex] === alphabet[i]) {
strIndex++;
} else {
missing = alphabet[i];
}
}
return missing;
}
console.log(missingLetter("abce"));
console.log(missingLetter("bcd"));
console.log(missingLetter("abcdefghjklmno"));
console.log(missingLetter("yz"));
Try this:
function fearNotLetter(str) {
var alp = ('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ').split(''), i;
for (i = alp.indexOf(str.charAt(0)); i < str.length; i++) {
if (str.split('').indexOf(alp[i]) === -1) {
return alp[i];
}
}
return undefined;
}
fearNotLetter('bcd');
Here's my solution, which i find to be quite easy to interpret:
function fearNotLetter(str) {
var missingLetter;
for (var i = 0; i < str.length; i++) {
if (str.charCodeAt(i) - str.charCodeAt(i-1) > 1) {
missingLetter = String.fromCharCode(str.charCodeAt(i) - 1);
}
}
return missingLetter;
}
I just did this challenge. I am a beginner to Javascript so this was my approach, very simple, sometimes you don't have to use the methods they provide but they also help. I hope you can understand it.
function fearNotLetter(str) {
var alphabet= "abcdefghijlmnopqrstuvwxyz";
var piece =alphabet.slice(0, str.length+1);
for(var i=0; i < piece.length; i++ ){
if(str.charCodeAt(0) != 97){
return undefined;
}
else if(str.indexOf(piece[i])===-1){
return piece[i];
}
}// for loop
}
fearNotLetter("abce");// It will return d
fearNotLetter("xy");//It will return undefined
fearNotLetter("bce");//It will return undefined