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.
Ich glaube, Sie haben einen Fehler im Code, vor dieser Zeile „Funktion Palindrom() {“ gibt es eine extra}. Prüfen – Kalamarico
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 –