freeCodeCamp Challenge Guide: Missing letters

This is my wonky solution. I didn’t use the recommended methods as I didn’t understand them. I’m sure this is probably a roundabout way to solve it but it works.

function fearNotLetter(str) {
  var letters = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"];
  var letterRange = letters.slice(letters.indexOf(str[0]),letters.indexOf(str.slice(-1)));
  var missingLetter = "";
  
 for (var i = 0; i < str.length; i++){
  if(str[i] !== letterRange[i]){
   missingLetter = letterRange[i];
      return missingLetter;
  }
   else if(str.slice(-1) === str[i]) {
     return undefined;
   }
 }
return letterRange;
}
fearNotLetter("abcdefghjklmno");
6 Likes

One more weird way:

function fearNotLetter(str) {
  var ascii = [];
  var sum = 0;
  var total = 0;
  var i = 0;
  for(i = 0; i < str.length; i++) {
    ascii[i] = str.charCodeAt(i);
    sum = sum + ascii[i];
  }
  
  ascii = ascii.sort(function(a, b){
    return a - b;
  });
  var length = ascii.length - 1;
  for(var j = ascii[0]; j <= ascii[length]; j++) {
    total = total + j;
  }
  
  total = total - sum;
  if(total === 0) {
    return;
  }
  
  return String.fromCharCode(total);
}

1 Like

Here is my simple(i think) solution:

function fearNotLetter(str) {
  var start = str.charCodeAt(0); //strcodes loop start
  var end = str.charCodeAt(str.length - 1); // strcodes loop end
  var strCodes = []; // there i will push every charCode from the start to the end of the string


for(var i = start; i < end; i++){
	strCodes.push(i);
}


 for(var b = 0; b < strCodes.length; b++){
  	if(String.fromCharCode(str.charCodeAt(b)) != String.fromCharCode(strCodes[b])){
    	return String.fromCharCode(strCodes[b]);
    }
  }
  return undefined;

}

//97-122
fearNotLetter("bcd");

1 Like

In my solution I use a loop to test first if the following char (i+1) would not be larger than the length of the str itself, because otherwise it means there was no gap and everything is in order and the outer return statement (outside of the loop) returns undefined. Within the loop I test if the following char (i+1) minus the current char would be anything else but 1, because a larger gap than 1 would mean that its not in line and something is missing. In this case it returns from the current char position the next in line.

function fearNotLetter(str) {
    for (var i = 0; i < str.length; i++) {
        if(i+1 < str.length && str.charCodeAt(i+1) - str.charCodeAt(i) !== 1) {
            return String.fromCharCode(str.charCodeAt(i)+1);
        }
    }
    return undefined;
}

fearNotLetter("abce");
1 Like

  • i got a little sneaky with this one
  • so since the char-codes go in order i just compared the code of the current letter +1 to the code of the next letter
  • if those two values aren’t equal i return the letter that should be there
  • if nothing qualifies for my if statement the function automatically returns undefined so i don’t even have to write that in the code
  • the loop stopping at str.length-1 was key here
  • javascript just blows my mind sometimes. this was just the first thing that came to mind and the fact that it works makes me psyched to do more
5 Likes

I feel like my functions are always so convoluted compared to others but I can’t seem to get out of thinking this way, it works but after looking at others I think I’ll take another crack at it

‘’'
function fearNotLetter(str) {
var alphabet = “abcdefghijklmnopqrstuvwxyz”;

var missingLetter;

if (alphabet.indexOf(str) === -1) {
missingLetter = undefined;
}

var firstLet = alphabet.indexOf(str[0]);

var alphaStr = alphabet.substr(firstLet, str.length).split(’’);

for (var i = 0 ; i < str.length ; i ++) {

if (str[i] !== alphaStr[i]) {

  missingLetter = alphaStr[i]; break;
}

}

return missingLetter;
}

fearNotLetter(“abcdefghjklmno”);
’’’

First time trying my solution, and it worked, although I misunderstood the purpose of this challenge…

function fearNotLetter(str) {

if(str[0] != ‘a’){
return;
}

var a = “abcdefghijklmnopqrstuvwxyz”.split(‘’);

for(var i=0;i<str.length;i++){
if(str[i] != a[i]){
return a[i];
}
}

}

doesnt actually check for a range of letters… More tests should be implemented before the challenge can be completed to check for this kind of thing? i.e. fearNotLetter(“fgjkl”)

2 Likes

Here is my solution:

function fearNotLetter(str) {

	var last;
	
	for (var i = 0; i < str.length; i++) {
		if (last && (str.charCodeAt(i) - last > 1)) {
			return String.fromCharCode(str.charCodeAt(i) - 1);
		}
		last = str.charCodeAt(i);
	}
}

// Test
fearNotLetter("abce");				// should return "d".
1 Like

This is my solution, similar to other ones before:

function fearNotLetter(str) {
  for(var i = 0; i < str.length-1; i++){
    if((str.charCodeAt(i) - str.charCodeAt(i+1)) !== -1){
      return String.fromCharCode(str.charCodeAt(i)+1);
    }
  }
  return undefined;
}

fearNotLetter("abcdefghjklmno");
1 Like

Said this works. Not sure if I believe it. It only seems to work if there is only one missing letter. Any thoughts?

function fearNotLetter(str) {
var a;
var num = [];
var missing = [];
var answer;
for (var i = 0; i<str.length; i++) {
num.push(str[i].charCodeAt());
}
var len = num[num.length-1];
for (var j = num[0]; j<len; j++) {
if (num.indexOf(j) == -1){
missing.push(j);
answer = String.fromCharCode(missing);
}
}

return answer;
}

fearNotLetter(“abcdefghjklmno”);

1 Like

Hi, everyone,

in addition to all of the solutions for this challenge. Here is my work:

[code]function fearNotLetter(str) {
var int = str.charCodeAt(0);
var x = 0;
while ( x < str.length) {
x++;
if (str.indexOf(String.fromCharCode(int++)) === -1) return String.fromCharCode(–int);
} return undefined;
}

fearNotLetter(“abce”);[/code]

1 Like
function fearNotLetter(str) {
  
  var newstr = str.toLowerCase().split('');
  var arr = "";
  
  if(newstr[0]==="a"){
    for(var i=0;i<=newstr.length;i++){
      if(i+1<newstr.length){
        num1 = parseInt(newstr[i].charCodeAt(0)) + 1;
        num2 = parseInt(newstr[i+1].charCodeAt(0));
        if(num1!=num2){
          arr = arr + String.fromCharCode(num1);
        }
      }
    }
    return arr;
  }
  
}

fearNotLetter("abcegi");

Cheers everyone,

Here’s my hair-brained and convoluted solution to this challenge:

function fearNotLetter(str) {
  
  var encoded = [],
      missing = [];
  
  for(var i = 0; i < str.length; i++){    
    encoded.push(str.split("")[i].charCodeAt());
  }
  for(var j = 0; j < encoded.length; j++) {
    var counter = encoded[j] - encoded[j-1],
        ref = 1;
    if(counter != 1)
      while(ref < counter) {
        missing.push(encoded[j-1]+ref);
        ref++;
      }
  }
  //-------------------------------number of missing elements----------------------------------
  function consecNumbers(encoded) {
    return encoded[encoded.length - 1] - encoded[0] - encoded.length + 1;
  }  // function that returns the number of elements missing in an array of consecutive numbers
  //-------------------------------------------------------------------------------------------
  if(consecNumbers(encoded) === 0){
    return undefined;
  }
  var found = String.fromCharCode(missing[0]);
  
  return found;
}

fearNotLetter("abce");

In my way to understanding the problem, I’ve introduced a new consecNumbers function that helped me to find out if from a given array of consecutive numbers, an element or more are missing. So because that was helpful for me and proved useful I incorporated it in the solution.

Hope I’ve added a new perspective for those interested.

function fearNotLetter(str) {
  var len = str.length;
 
  if(str.charCodeAt(len-1)-str.charCodeAt(0) === len-1){
    console.log(len,str.charCodeAt(len-1),str.charCodeAt(0));
    return undefined;
  }else{
    for(var i=0; i<len-1; i++){
      if(str.charCodeAt(i+1)- str.charCodeAt(i) !== 1){
        return String.fromCharCode(str.charCodeAt(i)+1);
      }
    }
  }
 
}
1 Like

I did not wanted to use for or while loop so I tried with arry.map
solution almost similar to intermediate solution provided here.

function fearNotLetter(str) {
var code=str.charCodeAt(0);

  return str.split('').map(function (x){
    if (x.charCodeAt(0)===code){
      code++;
    }else{
      return String.fromCharCode(code);
    }
  }
  )[str.length-1];
}
3 Likes

Tried this exercise with codePointAt and fromCharCode methods

        function fearNotLetter(str) {
           var newStr = []; // String Array to push Unicode of the given string
           var missingLetter;
  
          for (var i=0; i<str.length; i++) {
               newStr.push(str.codePointAt(i));
    
             // Checks the different between consequent indices,returns missing letter or                        
                                                   undefined accordingly
         for (var j=0; j<newStr.length; j++) {
             if (newStr[j+1] - newStr[j] > 1) {
                  missingLetter = newStr[j]+1;
                  return String.fromCharCode(missingLetter);
              } else {
                  str.codePointAt(missingLetter);
             }
         }
      }
   }

fearNotLetter("abd");