2017-10-04 7 views
0

私は、5桁の整数が回文かどうか、またはJavaScriptを使用していないかどうかをチェックしようとしていますが、失敗しています。私は5桁の文字列(整数ではない)を正しくチェックするコードを得ました。その部分は比較的簡単に把握できるはずです。JavaScriptを使用して整数の回文をチェックしています

私の主な質問:私はjavascriptで関数を使用するための構造をしていますか?もしそうなら、どのようにして関数呼び出しを正しく動作させることができますか?これは、所望の入力を受信すると、プログラムを終了するだけである。次のように

コードは次のとおりです。

<script type="text/javascript"> 
        var userInput; 
        var counter = 0; 

        function palindrome(userInput) { 
         var re = /[^A-Za-z0-9]/g; 

         userInput = userInput.toLowerCase().replace(re, ''); 
         var len = userInput.length; 

         for (var i = 0; i < len/2; i++) { 
          if (userInput[i] !== userInput[len - 1 - i]) { 
          return false; 
          } 
         } 
         return true; 
        } 

        userInput = window.prompt("Please enter a 5-digit, numerical palindrome: "); 
        palindrome(userInput); 

        while (counter < 10){ 
         if (userInput.length == 5){ 
          if (pandindrome(userInput) == true){ 
           document.write("The input is a palindrome!"); 
          } 
          else if (pandindrome(userInput) == false){ 
           document.write("The input is not a palindrome! Try again!"); 

           userInput = window.prompt("Please enter a 5-digit, numerical palindrome: "); 
           palindrome(userInput); 
          } 
         counter++; 
         } 
         else if (userInput.length != 5){ 
          alert("The input you entered was not 5-digits! Please try again!"); 

          userInput = window.prompt("Please enter a 5-digit, numerical palindrome: "); 
          palindrome(userInput); 
         } 
        } 
       </script> 
+0

あなたは 'palindrome'への最初の呼び出しの戻り値を何もしません。あなたは' isPalindrome'に名前を変更する必要があります。入力を文字列として扱うことを意図していますか? (整数が回文文字列に変換されずに文字列に変換されているかどうかをチェックするアルゴリズム的な解決策があるので)。 – Dai

+0

*アップデート*まあ、私は愚かだと感じる!私にはいくつかのスペルミスがありました。それは私が午前2時30分にコードを書こうとするときに得られるものです!私はまだ何か他のものではないことを確認するためにいくつかの問題をチェックしています。私は数字以外のすべてを除外する正しい構文を理解しているようには見えません。 – Rifleman192

+0

私は非常に奇妙なエラーが発生します!私が123456または5桁以外の他の連続番号を入力すると、クラッシュします。これは何ができるのでしょうか? – Rifleman192

答えて

0

私はあなたのコードをテストしている、すべてが正常に動作します!

FIY、関数palindromeからの戻り値を格納する変数isPalindromeを設定しました。そして、trueと比較する代わりに、if文の中で直接使用することができます。

var userInput; 
var counter = 0; 

function palindrome(userInput) { 
    var re = /[^A-Za-z0-9]/g; 

    userInput = userInput.toLowerCase().replace(re, ""); 
    var len = userInput.length; 

    for (var i = 0; i < len/2; i++) { 
    if (userInput[i] !== userInput[len - 1 - i]) { 
     return false; 
    } 
    } 
    return true; 
} 

userInput = window.prompt("Please enter a 5-digit, numerical palindrome: "); 
var isPalindrome = palindrome(userInput); 

while (counter < 10) { 
    if (userInput.length == 5) { 
    if (isPalindrome) { 
     document.write("The input is a palindrome!"); 
    } else { 
     document.write("The input is not a palindrome! Try again!"); 

     userInput = window.prompt(
     "Please enter a 5-digit, numerical palindrome: " 
    ); 
     isPalindrome = palindrome(userInput); 
    } 
    counter++; 
    } else if (userInput.length != 5) { 
    alert("The input you entered was not 5-digits! Please try again!"); 

    userInput = window.prompt("Please enter a 5-digit, numerical palindrome: "); 
    isPalindrome = palindrome(userInput); 
    } 
} 
関連する問題