2017-03-06 7 views
-3
<!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を決定するウェブサイトのために設計されていますが、私はなぜループが止まらず、何か助けをコードするようになったばかりです。単語が回文であるかどうかを判断する無限ループの問題

+2

中括弧で囲まれていないため、 'while'ループの唯一の文は' pall == true'です。 – bejado

+1

これはなんですか? 'while(x <= word1.length/2)(pall == true)' –

+0

これは実際にはブラウザのデバッグツールを使う素晴らしい機会です。実行中に一時停止するようにコードにブレークポイントを配置できるコードデバッガを見てから、コードが実行されるたびにコードをステップ実行して実行します。その行動は、あなたが期待しているものとどのように違うのですか? – David

答えて

2

ループを使用する必要がありますか?

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

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

isPalindrome(s1); 
isPalindrome(s2); 
+0

thx私はこれを使用しましたが、少し違っていました。私に正解を与えて、問題を抱えていたので、これに変更しました。 s1 =プロンプト( "単語を入力してください。 ); s2 = s1.split( ""); s3 = s2.reverse(); s4 = s3.join( ""); –

関連する問題