Missing letters challenge - is it good solution?

Missing letters challenge - is it good solution?
0.0 0

#1

Hi!

I’ve read lately that: If you are not able to get any feedback, your progress will be much slower

So I started this topic to get some feedback from you guys. :smile:

What do you think about my code for Missing Letters FCC challenge? Is it good solution? It works, but I don’t know if I made this solution the right way…

Example: http://repl.it/DMqa

function fearNotLetter(str) {
  var decodedArr = [];
  var upper = str.toUpperCase();
  //All elements in String to UpperCase
  var splittedInputData = upper.split('');  
  //UpperCase and splitted. Example: fearNotLetter("abce")->['A', 'B', 'C', 'E']
  for (var i in splittedInputData) {
    //For all elements in splittedInputData application is decoding UTF-16 values
    // and it is pushing them to decodedArr
    var decodedValue = splittedInputData[i].charCodeAt();
    decodedArr.push(decodedValue);
  }
  console.log('decodedArr:', decodedArr);
  var missingNumber = searchingForMissingNumber(decodedArr);
  //Function searchingForMissingNumber is looping through decodedArr and 
  //it is looking for missing number
  console.log('missingNumber:', missingNumber);
  function searchingForMissingNumber(input) {
    for(var k = 1; k < input.length; k++) {
      if(input[k] - input[k-1] != 1) {
        var difference = input[k] - input[k-1];
        var iWasLookingForYou = input[k] - 1;
        return iWasLookingForYou;
      }
    }
  }
  var lastCheckResult = lastCheck(missingNumber);
  //Function lastCheckResult is checking if missingNumber === undefined. 
  //If not it is returning myGoalReady.
  function lastCheck(item) {
    console.log('item:', item);
    if(item === undefined) {
      return undefined;
    } else {
        var myGoalUpper = String.fromCharCode(missingNumber);
        //Found missingNumber changes to UpperCase String
        var myGoalReady = myGoalUpper.toLowerCase();
        //UpperCase String modifies to LowerCase String
        return myGoalReady;
    }
  }
  return lastCheckResult;
}
fearNotLetter("abcdefghjklmno"); //should return "i".

#2

Hello !

Your code is good, overall. I just needed to clean some little things there and there :

function fearNotLetter(str) {
  var decodedArr = [];
  var splittedInputData = str.toUpperCase().split('');  
  
  for (var i in splittedInputData) {
    decodedArr.push(splittedInputData[i].charCodeAt());
  }
  
  var missingNumber = searchingForMissingNumber(decodedArr);
  
  function searchingForMissingNumber(input) {
    for(var k = 1; k < input.length; k++) {
      if(input[k] - input[k-1] != 1) {
        return input[k] - 1;
      }
    }
  }
  
  var lastCheckResult = lastCheck(missingNumber);

  function lastCheck() {
    if(missingNumber === undefined) {
      return undefined;
    } else {
        return String.fromCharCode(missingNumber).toLowerCase();
    }
  }
  return lastCheckResult;
}

fearNotLetter("abcdefghjklmno");

One thing that I find useful, for methods that all belongs to the same type (String, Arrays, etc) is that you can often chain them together instead of creating new variables :

so this

  var upper = str.toUpperCase();
  //All elements in String to UpperCase
  var splittedInputData = upper.split('');  

can become this :

  var upper = str.toUpperCase().split('');

I have done this variables cleanup in your code, just understand that it isn’t always needed to create new variables. Check out the return statements of both inner function, they can return the same thing !

Good job on your code !


#4

@Mizu
@P1xt

Thank you for your time and your kind words! Your feedback is helping me learn faster. Now I have a broader perspective on this issue. Thanks for replying again and spending part of your time to help me. :smiley: