admin管理员组

文章数量:1432230

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")

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")
Share Improve this question asked Feb 27, 2017 at 8:22 Colin SygielColin Sygiel 9456 gold badges19 silver badges31 bronze badges 9
  • 1 you set your variable i as a string but pare it with a number? – Amresh Venugopal Commented Feb 27, 2017 at 8: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 Commented Feb 27, 2017 at 8:26
  • aren't you meant to flip char[y].indexOf(str) i.e. str.indexOf(char[y]) why two loops? – Seabizkit Commented Feb 27, 2017 at 8:27
  • I was referrring to this part in the for loop: var i = char[0]; i < char.length – Amresh Venugopal Commented Feb 27, 2017 at 8:27
  • @AmreshVenugopal Good point, that doesn't make sense. – Colin Sygiel Commented Feb 27, 2017 at 8:36
 |  Show 4 more ments

2 Answers 2

Reset to default 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")

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");

本文标签: