2016-03-26 33 views
0

入力はアルファベット順の文字列です。どこかに手紙がありません。私は不足している文字を返す必要があります。私は以下のコードを投稿しました。それはよくコメントされており、それらのコメントはここで私ができるよりも優れています。私はその問題を以下で説明します。文字列に見つからない文字を見つける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"); 

ここに問題があります。私が "abce"を入力した場合、私はdを逃しています。 console.log(test)は2を返し、不足している文字を取得できます。すばらしいです。

"abcef"(最後にプラス記号fと同じ文字列)を入力すると、まだdがありません。テストは、文字が欠落していないと言っても1を返しますが、dはまだ失われています。

私のプログラムは、行方不明の文字が文字列の2番目の最後のスペースに収まる場合にのみ機能します。 "lmnp"は動作しますが、 "lmnpqrs"は動作しません。

私のループは、長い文字列 "abcdefghijklmnopqrstuvxyz"から欠落したwを取り除くことができるので、文字列の各文字を明瞭に反復しています。行方不明の文字の後に複数の文字があると、私のループが壊れてしまうのはなぜですか?あたかもループの外でconsole.log(test)を呼び出し、最後の反復だけを返すかのように動作します。私は代わりに配列にテストをプッシュしようとしましたが、それは助けにはなりません。

+0

コードを正しく読みやすくするために、字下げしてください。 – jfriend00

+0

Amanuel Bogalが編集しました。うまくいけば – PencilCrate

+0

@maraca "abcde" str.length = 5しかし、str.charCodeAtは[0]から[4]に変わります – PencilCrate

答えて

2

いくつかの問題があります:あなたが混ざっているインデックスを作成している(すなわち、オフ・バイ・1)。 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. 
+0

私は不必要に複雑にしていました。この解決策は機能します。ありがとう。 – PencilCrate

+0

関数の動作が関数の上のブロックで説明されていれば、コードはより明確になります。このフォームには非常に多くのインラインコメントがあり、ほとんど読めません。 '// iは1から始まり、str.lengthからインクリメントする 'のようなコメントは、ちょうど繰り返しであるようにはっきりと分かります。 – Hal50000

0

あなたのループブレークtestがある場合は、バック関数から戻ってきているためではない1より大きい

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" 
+0

Ooof。それはCrockPunch(TM) – Hal50000

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

お知らせは、関数が値を返します。

関連する問題