-2

I have a function to check if a word entered in a text input is a palindrome:

function Palindrome() {
  var revStr = "";
  var str = document.getElementById("str").value;
  var i = str.length;
  for (var j = i; j >= 0; j--) {
    revStr = revStr + str.charAt(j);
  }
  if (str == revStr) {
    alert(str + " is a palindrome");
  } else {
    alert(str + " is not a palindrome");
  }
}
<form>
  Enter a String or Number:
  <input type="text" id="str" name="string" />
  <br />
  <input type="submit" value="Check" onclick="Palindrome();" />
</form>

I would like the user to be prompted for a word, rather than entering a word into a text input, so I changed

var str = document.getElementById("str").value;

to

var str = prompt("Enter a string or number:")

But the prompt does not fire.
Why am I not being prompted for a word?

showdev
  • 28,454
  • 37
  • 55
  • 73
  • 3
    Remove the form and have `Palindrome();` after the closing `}` of your function to call the function. At the moment you're relying on the user clicking the button to run the function. – Andy Jul 13 '15 at 01:04
  • Also, [here's a slightly easier method to find out if a string is a palindrome](http://stackoverflow.com/a/14813569/1377002) that doesn't involve iteration. – Andy Jul 13 '15 at 01:08
  • So .. actually your question has nothing to do with palindromes and is in fact all about how to prompt the user to enter some text? Why didn't you say so in the question title then? – JK. Jul 13 '15 at 01:31

2 Answers2

1

You need to call Palindrome() at the end of your script. As it currently stands, Palindrome only fires when you click submit:

<script type="text/javascript">
    function Palindrome() {
        var revStr = "";
        var str = prompt("Enter a string or number:")
        var i = str.length;
        for(var j=i; j>=0; j--) {
            revStr = revStr+str.charAt(j);
        }
        if(str == revStr) {
            alert(str+" is a palindrome");
        } else {
            alert(str+" is not a palindrome");
        }
    }
    Palindrome()
</script>

JSFiddle

Richard Kho
  • 5,086
  • 4
  • 21
  • 35
  • If this question helped you solve your problem, you should accept this answer. Learn more about [accepting answers here](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work). – Richard Kho Jul 13 '15 at 03:13
0

You don't need the form if you're using a prompt, also there's a quick recursion way to find a palindrome (though not as efficient as the one referenced by Andy). I added it to your (working version) just for reference. Try this..

    function isPalindrome(x) {
        return x.length <= 1 ? true : (x.charAt(0) != x.charAt(x.length - 1) ? false : isPalindrome(x.slice(1, -1)))
    }

    function Palindrome(str) {

        var revStr = "";
       // var str = document.getElementById("str").value;
        var i = str.length;
        for(var j=i; j>=0; j--) {
            revStr = revStr+str.charAt(j);
        }

        if(str == revStr) {
            alert(str+" is a palindrome");
        } else {
            alert(str+" is not a palindrome");
        }
    }
    var str = prompt("Enter a string or number:")
    Palindrome(str)
    if (isPalindrome(str)){
        alert('isP: ' +str+" is a palindrome");

    } else{
        alert('isP: ' +str+" is not a palindrome");

    }
nril
  • 558
  • 2
  • 7