2017-10-04 44 views
0

我試圖檢查5位整數是否是使用javascript的迴文或未使用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,我設置了一個變量isPalindrome來存儲函數迴文的返回值。那麼你可以直接在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); 
    } 
}