1

this is my challenge: Create a function that will find the missing letter passed in the parameter and return it. If all letters are present in the string, the return will be undefined. For example missingLetter("abce") should return "d", missingLetter("bcd") should return undefined.

I am having trouble with this one, can you please tell me if I am on the right track with my code:

var missingLetter = function(char){
  var missing = "";
  var str = "abcdefghijklmnopqrstuvwxyz";
  for (var i = char[0]; i < char.length; i++){
     for(var y = char[0].indexOf(str); y < char.length; y++ ){
       if(char[y].indexOf(str) == -1 ){
         missing.push(char[y]);
       }
     }
  }
   console.log(missing);
  return missing;
}
missingLetter("abce")
Colin Sygiel
  • 917
  • 6
  • 18
  • 29
  • 1
    you set your variable `i` as a `string` but compare it with a `number`? – Amresh Venugopal Feb 27 '17 at 08:24
  • @AmreshVenugopal Are you referring to the .indexOf()? There I am using -1 to determine if this letter is not present in the array. If it is, then I push that missing letter to new variable. – Colin Sygiel Feb 27 '17 at 08:26
  • aren't you meant to flip `char[y].indexOf(str)` i.e. `str.indexOf(char[y])` why two loops? – Seabizkit Feb 27 '17 at 08:27
  • I was referrring to this part in the for loop: `var i = char[0]; i < char.length` – Amresh Venugopal Feb 27 '17 at 08:27
  • @AmreshVenugopal Good point, that doesn't make sense. – Colin Sygiel Feb 27 '17 at 08:36
  • @Seabizkit You are right. I am trying to use two for loops to compare the two, but you are saying I just need one? – Colin Sygiel Feb 27 '17 at 08:36
  • @ColinSygiel maybe, maybe not, my understanding is that you have "abc.." and you want to see if the char "x" is in it. "abc..".indexOf("x") would tell you. so you could have no loop. mmm rereading your question as i'm not getting what you want. why should `missingLetter("abce") should return "d"` or did you mean return dfghijklmnopqrstuvwxyz – Seabizkit Feb 27 '17 at 08:45
  • @Seabizkit In this case it should return "d" because "d" is the missing letter in the sequence of "abce" - Similarly, missingLetter("acd") should return "b". – Colin Sygiel Feb 27 '17 at 08:47
  • oh! i see you want it to match the sequence – Seabizkit Feb 27 '17 at 08:51

2 Answers2

2

Tonmoy already give the answer if you want you can check this. First if you want to use push function then you must create a array.

var missingLetter = function(char){
    var missing = []
    var y = 0
    var str = "abcdefghijklmnopqrstuvwxyz";
    for (var i = 0; i < str.length; i++){
        while(y  < char.length ){
            if( char[y] != str[y+i] ){
                missing.push(str[y+i])
                ++i
            }
            else
                ++y
        }
}
console.log(missing)
return missing
}
missingLetter("cdz")
Levent Saatci
  • 404
  • 3
  • 4
  • Cool! It makes sense to me, except for this line: char[y] != str[y+i], especially what does str[y+i] doing? – Colin Sygiel Feb 27 '17 at 09:25
  • hi its because you must change str element every time but you must look for z element every time – Levent Saatci Feb 27 '17 at 09:29
  • Hmm, what would str[y+i] = at any time? for instance what does str[y+j] = when char[y] = "c"? – Colin Sygiel Feb 27 '17 at 09:38
  • when char[y] = c this means y = 0 and i = 2 and char[y] and str[i+y] are equals. if i = 5 and y still 2 str[i+y] = h char[y] = z which still not equal then add 1 to i i become 6 and y =2 str[i+y] = i – Levent Saatci Feb 28 '17 at 10:40
0

you have defined variable missing as string, but It should be a array(). The loop condition is not properly. Following is the code snippet, which works fine.

var missingLetter = function(char){
    var missing = new Array();
    var str = "abcdefghijklmnopqrstuvwxyz";
    var i = 0;
    while(i<char.length) {
        for(var j=0;j<26;j++) {
            if(str[j].indexOf(char[i])>-1){
                i++;
            } else {
                missing.push(str[j]);
            }
        }
    }
    console.log(missing);
    return missing;
}
missingLetter("abce");
Tonmoy
  • 91
  • 3
  • Hey thanks Tonmoy, this works but isn't quite exactly it. In this your code is returning all missing letters "d" + "f-z" however I just want to return "d" - the only missing letter in the inputted string. – Colin Sygiel Feb 27 '17 at 09:14
  • Then you need to change few lines. Replace it if(str[j].indexOf(char[i])>-1){ i++; } else { missing.push(str[j]); } with if(str[j].indexOf(char[i]) == -1){ missing.push(str[j]); return missing; } – Tonmoy Feb 27 '17 at 10:17