2017-02-03 3 views
2

자바 스크립트 간단한 회문 기능은 </p> <p>

function palindrome(str) { // "Hello there" 
 
    str.toLowerCase(); // "hello there" 
 
    str = str.replace(/\s/g, ""); // "hellothere" 
 
    var a = str; 
 
    a.split("").reverse().join(""); // a = "erehtolleh" 
 
    return (str === a); // "hellothere" === "erehtolleh" 
 
} 
 

 
alert(palindrome("123432"));
나는 비 -palindromic 값 123432을 통과하지만, 사실 값을 반환합니다. 내 palindrome 기능에 문제가있는 사람은 누구입니까? 누군가 내 논리를 점검 할 수 있다면 정말 고맙겠습니다. 당신이 변수에 반전 된 문자열을 할당해야 또는 비교 않도록

답변

4

당신은

function palindrome(str) {      // "Hello there" 
 
    str.toLowerCase();        // "hello there" 
 
    str = str.replace(/\s/g, "");     // "hellothere" 
 
    var a = str;        
 
    a = a.split("").reverse().join("");     // a = "erehtolleh" 
 
    return (str === a);        // "hellothere" == "erehtolleh" 
 
} 
 

 

 
alert(palindrome("malayalam"));

+0

왜이 변질 "말라얄람어은"? 'palindrome' 함수를 호출하는 것을 의미합니까? –

+0

오 예, 기능을 호출하는 것을 잊었습니다; 0) –

1

이를 시도해보십시오 리턴 기능을 자사로의 값을 할당 할 필요가 문자열 immuatble입니다 원래와 반전 된 String.

function palindrome(str) { // "Hello there" 
 
    str.toLowerCase(); // "hello there" 
 
    str = str.replace(/\s/g, ""); // "hellothere" 
 
    // var a = str.split("").reverse().join(""); // a = "erehtolleh" 
 
    return (str === str.split("").reverse().join("")); // "hellothere" == "erehtolleh" 
 
} 
 

 

 
alert(palindrome("12321")); 
 
alert(palindrome("1232"));