Intermediate Algorithm Scripting: Missing letters (return statement)

Tell us what’s happening:
I got it to pass but is this an OK way to write it? All these Variable? Good Part was I console logged the whole way.

Your code so far


function fearNotLetter(str) {
var result = "";
var alpha = "abcdefghijklmnopqrstuvwxyz";
var Luigi = alpha.split("");
var Mario = str.split("");
var start = Luigi.indexOf(Mario[0]);
var MarioHt = Mario.length + 1;
var LuigiJr = Luigi.splice(start, MarioHt);
var missing = "";

for (var i = 1; i <= Mario.length; i++) {
if (Mario.indexOf(LuigiJr[i]) == -1) {
missing = i ;
}
}
return LuigiJr[missing];
}

fearNotLetter("stvwx");

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.105 Safari/537.36.

Challenge: Missing letters

Link to the challenge:

Nothing wrong with having a lot of variables, as long as you keep them named well, which is a problem here. Code itself might be improved too:

function fearNotLetter(str) {
  var result = ""; // NEVER USED
  var alpha = "abcdefghijklmnopqrstuvwxyz"; // OK
  var Luigi = alpha.split(""); // Luigi?? Unnecessary step
  var Mario = str.split(""); // Mario?? Unnecessary step
  var start = Luigi.indexOf(Mario[0]);  // Swap Luigi with alpha and Mario with str and result will be same
  var MarioHt = Mario.length + 1; // NO
  var LuigiJr = Luigi.splice(start, MarioHt); // Use slice instead, so you can skip converting to array
  var missing = ""; // Unnecessary

  /** Keep in mind that LuigiJr and str start with same character and have same length */
  for (var i = 1; i <= str.length; i++) { // str, not Mario
    if (Mario.indexOf(LuigiJr[i]) == -1) { // Do you still keeping that in mind? Try to find quicker way to compare characters
      missing = i ; // Let's say you found it! No need to continue the loop, you can return right away
    }
  }
  return LuigiJr[missing]; // Here you will return fallback instead ('') if missing character hasn't been found
}

Good luck!

I always use Mario and Luigi when comparing strings/arrays of different length. I just have to remember Mario is the runt and Luigi is the stringBean of the family. You’re right, I could have condensed some of them, but I was trying to isolate the bugs. Ran into a lot of “XYZ.method() is not a function” .Slice didn’t work so I fired it (probably b/c its an array?)- added the “p” and it worked immediately. Performing functions on the ARG using a “copy” feels legit b/c of mutation issues, but I can probably rename them after I get it to pass.
also, regarding LuigiJr and str, its the section of the alphabet being searched so it should equal str+1. It contains the letter str is missing. Are you implying that .find() would be a better function here?

Thanks for the feedback.

var Luigi = "abcdefghijklmnopqrstuvwxyz".split(""); //combining
Var MarioHt = Mario.length + 1; // probably just str.length +1?

Variable names are not just for you. They are for others reviewing code you write. Variable names should describe what the values represent and function names should describe what the function does. This is the key to readable code.

1 Like

Let’s do few examples. First of all most of the times when you work with the strings you don’t really need to convert them into arrays. They have methods for pretty much anythings:

const str = 'Hello world';
const world = str.slice(str.indexOf('w'));
console.log(world);

Secondly, often pen and paper clear the tasks - if you write down two strings that start with the same letter and have ascending order (one with missing character) with indexes above you will get something like this, right?

index (i)  0   1   2   3   4
string A   f   g   h   j   k
string B   f   g   h   i   j   k   ...   z

Do you see MUCH simpler way to compare characters at the same index rather than with indexOf?

When you wrote “Let’s say you found it!” I thought you were hinting.

Great point about pen and paper. I often write out the solutions by hand after I pass. So thank you again for so generously giving your time!

const str = 'Hello world';
const world = str.slice(str.indexOf('w'));
console.log(world);// logs world...changing the 'w' to 'W' logs d (shrug-head-scratch)

second example:

var A = "FGHIJK"
var B = "FGHJK";
for (var i = 0; i < A.length; i++)
for(var j = 0; j < B.length; j++)
return B.charCodeAt[j]  !== A.charCodeAt[i];

In answer to your question, I’ve spent several hours on this with .search(), .filter() .match() but I am still struggling with return, console.log(), and general debugging. So its hard to know. I am throwing a lot of things at it and its hard what works and what doesn’t.

You don’t need two loops here as you need to check if A[0] === B[0], then A[1] === B[1] etc. number in square brackets will always be the same.

Regarding return, the way you wrote it you will return boolean (either true or false) right away which isn’t what challenge asks you. If A[i] !== B[i] then you need to return A[i], the missing character, right?

1 Like

This makes sense but I don’t know how to loop through both arrays with a single index variable. what I have found in researching code online is over my head and does’t seem appropriate. I guess methods likeforEach() can do it but then would I even need the loop? Well here is a stab

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