2017-09-20 1 views
0

Ich muss überprüfen, ob ein Text ein Palindrom ist oder nicht, durch das Konzept der Stapel habe ich eine Funktion, die den Stapel erstellt, und eine andere, die prüft, ob es ein Palindrom ist, das das Wort stapelt und entstapelt.Wie überprüft man Palindrome mit FILO in JavaScript?

Das Problem ist, dass ich nicht weiß, wie ich diese Überprüfung durchführen kann.

Mein Code:

<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> 

Aber dieser Code nicht funktioniert, weil, wenn er sagt, die Eingabe, dass keine Gegenstände in dem Stapel sind, und sie nicht am Ende wird eingeführt, so kann ich unstack. Wie kann ich das schaffen?

PS: Ich bin neu in JavaScript, so dass der Code eine unordentliche Sache sein kann, sorry dafür.

+0

Ich glaube, Sie haben einen Fehler im Code, vor dieser Zeile „Funktion Palindrom() {“ gibt es eine extra}. Prüfen – Kalamarico

+0

es versucht, die Buchstaben angehängt Text2 das Wort rückwärts zu erstellen und dann zu vergleichen 'var text2 =„“; text2 + = mystack.Pop(); '' dann if (text1 === text2) 'Sie haben ein Palindrom –

Antwort

1

Sie sind eigentlich ganz in der Nähe. Sie müssen nur concatenate die knallte Buchstaben zusammen und vergleichen Sie dann auf text1 (die Sie bereits tun).

Sie haben auch einen Fehler in Ihrer zweiten do while Sie verwenden den gleichen Iterator i als erste Schleife, wenn Sie wahrscheinlich Ihre t Variable verwenden und dekrementieren sollte. Hier ist das Beispiel mit dem festen Code

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

Danke, es hat perfekt funktioniert. So war es am Ende, einige kleine Fehler, ich sah gerade jetzt, dass ich die Variable t nicht benutzte. Wie auch immer, danke nochmal. – Monteiro

0

versuchen Sie dies:

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

Hallo, danke für den Code, aber ich habe die Funktion FILO, in der Funktion Palindrom verwenden, zum Beispiel unter Verwendung von . Also müsste es mystack.stack1.push sein. Könnte es so funktionieren? Weil ich die Funktion FILO instanziieren muss. – Monteiro

Verwandte Themen