2017-09-20 5 views
0

私は、テキストがパリンドロームかどうか、スタックのコンセプトを通して、スタックを作成する関数を持っていて、それが単語を積み重ねて積み重ねる回文かどうかを調べるかどうかをチェックする必要があります。JavaScriptでパリンドロームをFILOでチェックするには?

問題は、私がこの検証をどのように進めることができないかということです。

マイコード:

<html> 

<head> 
    <script type ="text/javascript" /> 
     function FILO(){ 
       this.stack = new Array(); 

       this.Push = function(obj){ 
       this.stack[this.stack.length] =obj; 
       } 

     this.Pop =function(){ 
       if(this.stack.length>0){ 
        var obj = this.stack[this.stack.length - 1]; 
        this.stack.splice(this.stack.length -1,1); 
        return obj; 
       }else { 
       alert("Theres no objects in the stack"); 

      } 
      } 
     } 

     function palindrome() { 
     var mystack = new FILO(); 
     var text1; 
     var text2; 
     var i; 
     var t; 
     text1 = prompt("Type a text: "); 
     i = text1.length; 
     t = text1.length; 

     do{ 
      mystack.Push(text1.substr(t-i,1)); 
      i--; 
     }while(i>0); 

     do{ 
      text2 = mystack.Pop(); 
      document.write(text2, "</br>"); 
     }while(i>0); 

     if(text1 === text2) { 
     alert("It is a palindrome"); 
     } 
     else { 
     alert("It's not a palindrome"); 
     } 

     } 



    </script> 

</head> 


<body> 
<h1>Verification of Palindrome </h1> 
<p>Press the button to see if a word is a palindrome or not</p> 

<form> 
    <input type = "button" onClick ="palindrome()" value = "Verifiy"> 
</form> 

</body> 
</html> 

しかし、このコードは動作しませんが、入力するとき、それは何のオブジェクトがスタックに存在しないことを言うと、彼らは私がスタックを解除できるように挿入されてしまういないため。どうすればこの作品を作れますか?

PS:私はJavaScriptが新しく、コードは面倒なことになる可能性があります。申し訳ありません。

+0

との一例である私は、「{余分がある})(関数回文」この行の前に、あなたは、コードに誤りがあると思います。それを確認してください – Kalamarico

+0

文字を後方に作成し、次に 'var text2 =" "; (テキスト1 ===テキスト2) 'あなたが回文を持っている場合 –

答えて

1

あなたは実際には非常に接近しています。 は、ポップされた文字をに連結してから、text1(これはすでに行っています)と比較するだけです。

のバグがあります。t変数を使用している可能性があり、デクリメントする必要がある場合は、同じイテレータiを最初のループとして使用します。ここにあなたの固定コード

function FILO() { 
 
    this.stack = new Array(); 
 

 
    this.Push = function(obj) { 
 
    this.stack[this.stack.length] = obj; 
 
    } 
 

 
    this.Pop = function() { 
 
    if (this.stack.length > 0) { 
 
     var obj = this.stack[this.stack.length - 1]; 
 
     this.stack.splice(this.stack.length - 1, 1); 
 
     return obj; 
 
    } else { 
 
     alert("Theres no objects in the stack"); 
 

 
    } 
 
    } 
 
} 
 

 
function palindrome() { 
 
    var mystack = new FILO(); 
 
    var text1; 
 
    var text2 = ""; 
 
    var i; 
 
    var t; 
 
    text1 = prompt("Type a text: "); 
 
    i = text1.length; 
 
    t = text1.length; 
 

 
    do { 
 
    mystack.Push(text1.substr(t - i, 1)); 
 
    i--; 
 
    } while (i > 0); 
 

 
    do { 
 
    text2 += mystack.Pop(); //Here this should be += instead of = 
 
    t-- 
 
    document.write(text2, "</br>"); 
 
    } while (t > 0); //use and decrement t variable in this do while 
 

 
    if (text1 === text2) { 
 
    alert("It is a palindrome"); 
 
    } else { 
 
    alert("It's not a palindrome"); 
 
    } 
 

 
}
<html> 
 
<head> 
 
</head> 
 
<body> 
 
    <h1>Verification of Palindrome </h1> 
 
    <p>Press the button to see if a word is a palindrome or not</p> 
 
    <form> 
 
    <input type="button" onClick="palindrome()" value="Verifiy"> 
 
    </form> 
 
</body> 
 
</html>

+0

ありがとう、それは完全に働いた。だから最終的には、いくつかの小さなエラー、私は変数tを使用していなかった今見た。とにかく、もう一度ありがとう。 – Monteiro

0

はこれを試してみてください。

var stack1 = [], stack2 = []; 
var text1 = prompt('Type a text: '); 
for(var i = 0, max = text1.length; i < max; i++) { 
    stack1.push(text1[i]); 
    stack2.push(text1[max - 1 - i]); 
} 

var isPalindrome = true; 
while(stack1.length > 0) { 
    if(stack1.pop() != stack2.pop()) { 
     isPalindrome = false; 
     break; 
    } 
} 

console.log('isPalindrome: ', isPalindrome); 
+0

こんにちは、ありがとうございますが、関数の回文では、関数FILOを使用する必要があります。 。だからそれはmystack.stack1.pushでなければなりません。それはそのように働くことができますか?関数FILOをインスタンス化する必要があるためです。 – Monteiro

関連する問題