在字符串Javascript

时间:2016-03-26 21:51:27

标签: javascript

因此输入是按字母顺序排列的字母串。在那里的某处,丢失了一封信。我必须把丢失的信寄回去。我在下面发布了我的代码。评论很好,这些评论比我在这里解释得更好。我将在下面解释我的问题。

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

这就是问题所在。如果我输入&#34; abce&#34;我很想念。 console.log(test)返回2,我可以得到丢失的信。大。

如果我输入&#34; abcef&#34; (与之前相同的字符串加上结尾的f)我仍然缺少d。测试返回1,好像说没有字母丢失,但d仍然缺失。

我的程序仅在缺少的字符适合字符串的倒数第二个空格时才有效。 &#34; LMNP&#34;工作,但&#34; lmnpqrs&#34;不起作用。

我的循环显然正在遍历字符串中的每个字符,因为它可以从长字符串中选出缺少的字符串&#34; abcdefghijklmnopqrstuvxyz&#34;。当缺少字符后有多个字符时,为什么我的循环会崩溃?它的行为就像我在循环之外调用console.log(test),并且只返回最后一次迭代。我尝试将测试推送到阵列,但这对任何人都没有帮助。

7 个答案:

答案 0 :(得分:2)

有几个问题:你的索引是混淆的(即逐个);你undefined的回归应该在循环之外,而不是在循环中;你在不应该使用的地方使用str.length;你不应该把迭代变量放在括号中:

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.

答案 1 :(得分:1)

/*
 * 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;
}

请注意,在这两种情况下,函数都返回一个值。

答案 2 :(得分:1)

您可以使用这种简单的解决方案

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

答案 3 :(得分:0)

你的循环中断是因为如果test不大于1,你将从函数返回。

答案 4 :(得分:0)

以下是我对此的看法:

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"

答案 5 :(得分:0)

大家好!

我仍然从奶瓶中消费我的JS。我想出了一个解决方案:

希望你喜欢

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 incomplete 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 incomplete sequence
      if (str.charAt(j) !== alphaStr.charAt(j + strIndex)){//corresponding iteration matches on the alphabet start where the incomplete 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')

答案 6 :(得分:0)

按如下所示操作:

dynamic_key_2