0

I need help in finding solution to know a string is pallindrome or not , this seems to be a simple one but i need to know if we can make it in a single line code. I tried to use

var numstr = "121"
function checkPallindrome() {
    var check = num.split("").reverse.join();
    alert(check);
} 

But no luck, please guide me if had any mistakes in the code I have written

6
  • check this.............stackoverflow.com/questions/22111507/… Commented Nov 7, 2014 at 11:44
  • 1
    What do you mean by "no luck"? What happened? What didn't happen? Be scientific. Commented Nov 7, 2014 at 11:44
  • code is wrong. num is not defined, missing semi-colon on the first line, probably you want some parameter for your function... Commented Nov 7, 2014 at 11:45
  • @tomasb: Semicolons are optional. Commented Nov 7, 2014 at 11:45
  • 1
    As a side note, it's called palindrome, not pallindrome Commented Nov 7, 2014 at 11:47

1 Answer 1

0

There are many formatting errors in your code. It should be something like:

function checkPallindrome(num) { 
  var check = num.split("").reverse().join('');
  return num == check;
} 

Things that went wrong in your function:

  • You didn't include any parameter in your function
  • you forgot to add () to reverse
Sign up to request clarification or add additional context in comments.

22 Comments

Full working answers to homework questions are not helpful to learners in the long-run
The correct answer to work with all implementation is : Do not press enter, it will be in one line (sorry joke)
also it may be better to create loop and compare first char with the last and so on to the middle of the string
@tomasb Yes, there are better ways to write the function. I'm just trying to fix OP's mistakes.
@tomasb: What nonsense is this?! ""performance which is already poor with JavaScript"" yeah in the early 1990s perhaps. This code is clearer and more expressive than yet another bleeding for loop. And the complexity isn't going to decrease just because you write out the inner loop by hand.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.