无限循环问题,用于确定单词是否为回文结构

时间:2017-03-06 01:39:56

标签: javascript

<!DOCTYPE html>
<html lang="en">

<head>
 <meta charset="UTF-8">
 <title>Programming Task 1</title>
</head>

<body>
 <script>
 var x, pall, word1;
 word1 = prompt("Please enter a word and i will determine if its a palindrome or not")
 x = 0
 while( x <= word1.length / 2)( pall == true)
    if (word1.length(x)!= word1.charAt(word1.length - 1 - x)){
        pall == false
    }

    x=x+1;
 if (pall == true) {
    confirm("the word you entered "+ word1 + " is a palindrome")
 }
 else
 {
    confirm("the word you entered " + word1 + " is not a palindrome")
 }
 </script> 

</body>

</html>

这就是我目前所拥有的。这是专门为一个网站确定一个Palindrome,我不确定为什么,但循环没有停止,我只是开始编码任何帮助,赞赏

1 个答案:

答案 0 :(得分:2)

你必须使用循环吗?使用javascript有一种更简单的方法:

var s1 = 'racecar';
var s2 = 'notracecar';

function isPalindrome(string){
    return string.split('').reverse().join('') == string;
}

isPalindrome(s1);
isPalindrome(s2);