2017-10-04 3 views
0

5 자리 정수가 palindrome인지 또는 JavaScript를 사용하지 않는지 확인하지 않으려 고 시도하고 있습니다. 5 자리 문자열 (정수가 아님)을 올바르게 확인하는 코드를 얻었습니다. 그 부분은 상대적으로 쉽게 파악할 수 있어야합니다.자바 스크립트를 사용하여 정수 palindrome 확인

내 주요 질문 : 자바 스크립트에서 함수를 사용하기위한 구조를하고 있습니까? 그렇다면 함수 호출을 제대로 작동 시키려면 어떻게해야합니까? 원하는 입력을 수신하면 프로그램을 종료합니다. 다음과 같이

코드는 다음과 같습니다

난 당신의 코드를 테스트 한
<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); 
    } 
} 
관련 문제