最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Find missing letter in string Javascript - Stack Overflow

programmeradmin1浏览0评论

So the input is a string of letters in alphabetical order. Somewhere in there, a letter is missing. I must return the missing letter. I have posted my code below. It is well mented and those ments explain better than I can here. I will explain my problem below that.

function fearNotLetter(str) {
  var charCodes;
  var test;
  var offendingNumber;
  var numToString;

  // i starts at 1, increments to str.length
  for (var i = 1; i < str.length; i++) {
    // Char code of last letter minus char code of second last letter,
    // Char code of second last letter minus char code of third last letter, etc.
    // Repeat on a loop and set result equal to test each time.
    test = str.charCodeAt(str.length - [i]) - str.charCodeAt(str.length - [i + 1]);
    console.log(test);

    // If charCode A - charCode B == 1, then letters are in order
    // alphabetically and test returns 1. 

    // If charCode A - charCode B > 1, then letters missing from string.

    // So if difference between char codes is more than 1,
    // return missing char code and convert to string.
    if (test > 1) {
      offendingNumber = str.charCodeAt(str.length - [i]);
      numToString = String.fromCharCode(offendingNumber);
      console.log(numToString);
    } // End of if.
    // If no letters missing from input, return undefined.
    else {
      return undefined;
    } // End of else.
  } // End of loop.
} // End of function.

// Here is the input str
fearNotLetter("abce");

Here's the problem. If I input "abce" I am missing d. console.log(test) returns 2, and I can get the missing letter. Great.

If I input "abcef" (same string as before plus f on the end) I am still missing d. Test returns 1 as if to say no letter is missing, but d is still missing.

My program only works if the missing character would fit in the second last space in the string. "lmnp" works, but "lmnpqrs" does not work.

My loop is clearly iterating through each char in the string, because it can pick out the missing w from the long string "abcdefghijklmnopqrstuvxyz". Why does my loop break down when there are multiple characters after the missing character? It's behaving as if I called console.log(test) outside of the loop, and only returning the last iteration. I've tried pushing test to an array instead, but that doesn't help any.

So the input is a string of letters in alphabetical order. Somewhere in there, a letter is missing. I must return the missing letter. I have posted my code below. It is well mented and those ments explain better than I can here. I will explain my problem below that.

function fearNotLetter(str) {
  var charCodes;
  var test;
  var offendingNumber;
  var numToString;

  // i starts at 1, increments to str.length
  for (var i = 1; i < str.length; i++) {
    // Char code of last letter minus char code of second last letter,
    // Char code of second last letter minus char code of third last letter, etc.
    // Repeat on a loop and set result equal to test each time.
    test = str.charCodeAt(str.length - [i]) - str.charCodeAt(str.length - [i + 1]);
    console.log(test);

    // If charCode A - charCode B == 1, then letters are in order
    // alphabetically and test returns 1. 

    // If charCode A - charCode B > 1, then letters missing from string.

    // So if difference between char codes is more than 1,
    // return missing char code and convert to string.
    if (test > 1) {
      offendingNumber = str.charCodeAt(str.length - [i]);
      numToString = String.fromCharCode(offendingNumber);
      console.log(numToString);
    } // End of if.
    // If no letters missing from input, return undefined.
    else {
      return undefined;
    } // End of else.
  } // End of loop.
} // End of function.

// Here is the input str
fearNotLetter("abce");

Here's the problem. If I input "abce" I am missing d. console.log(test) returns 2, and I can get the missing letter. Great.

If I input "abcef" (same string as before plus f on the end) I am still missing d. Test returns 1 as if to say no letter is missing, but d is still missing.

My program only works if the missing character would fit in the second last space in the string. "lmnp" works, but "lmnpqrs" does not work.

My loop is clearly iterating through each char in the string, because it can pick out the missing w from the long string "abcdefghijklmnopqrstuvxyz". Why does my loop break down when there are multiple characters after the missing character? It's behaving as if I called console.log(test) outside of the loop, and only returning the last iteration. I've tried pushing test to an array instead, but that doesn't help any.

Share Improve this question edited Mar 26, 2016 at 22:06 PencilCrate asked Mar 26, 2016 at 21:51 PencilCratePencilCrate 2111 gold badge4 silver badges14 bronze badges 8
  • Please indent your code properly to make it readable. – jfriend00 Commented Mar 26, 2016 at 21:53
  • Amanuel Bogal edited it. Hopefully that's nicer to read – PencilCrate Commented Mar 26, 2016 at 21:55
  • @maraca "abcde" str.length = 5 but str.charCodeAt goes from [0] to [4] – PencilCrate Commented Mar 26, 2016 at 22:03
  • this code is horrible but apart from that you're missing a thing in the if statement, the return to be exact – Nhor Commented Mar 26, 2016 at 22:04
  • @Nhor thanks. I know that return is not in the if statement. I will add it just to be clear that's not the problem – PencilCrate Commented Mar 26, 2016 at 22:06
 |  Show 3 more ments

7 Answers 7

Reset to default 2

There are several problems: you're indexing is mixed up (i.e. off-by-one); your return of undefined should be outside the loop, not in it; you're using str.length in places you shouldn't; you're putting the iteration variable into brackets when you shouldn't:

function fearNotLetter(str) {
    var difference;
    var missingCharCode;

    // i starts at 1, increments to str.length
    for (var i = 1; i < str.length; i++) {

        // Char code of last letter minus char code of second last letter,
        // Char code of second last letter minus char code of third last letter, etc.
        // Repeat on a loop and set result equal to test each time.
        difference = str.charCodeAt(i) - str.charCodeAt(i - 1);

        // If charCode A - charCode B == 1, then letters are in order
        // alphabetically and test returns 1. 

        // If charCode A - charCode B > 1, then letters missing from string.

        // So if difference between char codes is more than 1,
        // return missing char code and convert to string.
        if (difference > 1) {
            missingCharCode = str.charCodeAt(i) - 1;
            return String.fromCharCode(missingCharCode);
        } // End of if.
    } // End of loop.

    return undefined;
} // End of function.
/*
 * Returns the first non alphabetic character in the input string. If 
 * all characters are in alphabetic order function returns null.
 */
function findFirstNonAlphabeticCharIn(input) {

    for (var i = 1; i < input.length; i++) {

        var range = input.charCodeAt(i) - input.charCodeAt(i - 1);
        if (range != 1) return input.charAt(i);    
    }
    return null;
}

Notice in both cases the function returns a value.

Here's my take at it:

function fearNotLetter( str ) {
  var ch0 = str.charCodeAt(0), ch;
  str.split("").every(function(v, i){
    ch = String.fromCharCode(ch0 + i);
    return ch === v;
  });
  return ch === str[str.length-1] ? undefined : ch;
}


console.log( fearNotLetter("cdefgij") ); // "h"

You can just use this simple solution

function fearNotLetter(str){
    let num = 97;
    for(let s of str){
        if(s.charCodeAt(0) !== num) return String.fromCharCode(num);
        num++;
    }
    return String.fromCharCode(num)  
}

Your loop breaks because you are returning back from the function if test is not greater than 1.

Good day all

I am still consuming my JS from a milk bottle.I came up with a noob solution to this one:

Hope you like it

Blockquote

function fearNotLetter(str) {
  //STRATEGY
  //1. create a string of the whole alphabet
  //2. locate the starting point of str in the alphabet string

  let alphaStr = 'abcdefghijklmnopqrstuvwxyz';
  let strIndex = alphaStr.match(str.charAt(0)).index;//on what index does the first letter of the inplete sequence lie in the alphabet string
  console.log(strIndex); 
    for (var j = 0; j < str.length; j++) {//limit the iteration to the length of the inplete sequence
      if (str.charAt(j) !== alphaStr.charAt(j + strIndex)){//corresponding iteration matches on the alphabet start where the inplete string start
        console.log(alphaStr.charAt(j + strIndex));
        return alphaStr.charAt(j + strIndex);        
        } 
  }
}
let alphaStr = 'abcdefghijklmnopqrstuvwxyz';
let testStr = 'stvwx';
let testIndex = alphaStr.match(testStr.charAt(0)).index;
console.log(`THE CHARACTER AT INDEX ${testIndex} IS ${alphaStr.charAt(testIndex)}`)
fearNotLetter("abce");
fearNotLetter('abcdefghjklmno');
fearNotLetter('bcdf');
fearNotLetter('stvwx');
fearNotLetter('abcdefghijklmnopqrstuvwxyz')

Do it like below:

let value = "ghijklmnoqstuw" //missing p,r,v

//find first missing character
function findOdd(test){
    let abc = "abcdefghijklmnopqrstuvwxyz"
  let sp = abc.indexOf(test[0])
  for(let i = 0;i<test.length;i++){
    if(test[i]!==abc[sp+i]){
      console.log(abc[sp+i]);
      //sp++; i--; //unment to find all missing character
      break; // ment to find all missing character
    }
  }
}

findOdd(value)
发布评论

评论列表(0)

  1. 暂无评论