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

Balázs Édes
  • 13,452
  • 6
  • 54
  • 89
Angular Learner
  • 390
  • 4
  • 16

1 Answers1

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
RRikesh
  • 14,112
  • 5
  • 49
  • 70
  • Full working answers to homework questions are not helpful to learners in the long-run – Jamiec Nov 07 '14 at 11:47
  • The correct answer to work with all implementation is : Do not press enter, it will be in one line (sorry joke) – Tony Nov 07 '14 at 11:49
  • 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 Nov 07 '14 at 11:50
  • @tomasb Yes, there are better ways to write the function. I'm just trying to fix OP's mistakes. – RRikesh Nov 07 '14 at 11:52
  • @Jamiec I edited my answer to add necessary information. – RRikesh Nov 07 '14 at 11:53
  • @RRikesh I think you misunderstood my comment. – Jamiec Nov 07 '14 at 11:55
  • @Jamiec Sorry about that... – RRikesh Nov 07 '14 at 11:56
  • @tomasb: I prefer this one. – Lightness Races in Orbit Nov 07 '14 at 12:13
  • @lightness: why? if it is used in a loop it must have impact on performance which is already poor with javascript... does it look cool? :) – tomasb Nov 07 '14 at 12:21
  • 1
    @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. – Lightness Races in Orbit Nov 07 '14 at 12:27
  • @lightness: performance is poor, single thread (2 for administration of intervals) being interrupted with container... but if you think it is ok im not gonna argue on that; the line you prefer will split into several loops which is more things to do then just one loop; i agree it is more expressive but it can be achieved with a comment as well – tomasb Nov 07 '14 at 12:32
  • @tomasb: Lol take a course on algorithms mate. – Lightness Races in Orbit Nov 07 '14 at 12:44
  • @lightness: you learn count to 3 mate, how about the final comparison of strings? it is atomic or what? also you can tell not a palindrom after first character comparison, lol to yourself silly or just make a test – tomasb Nov 07 '14 at 12:49
  • @tomasb http://en.wikipedia.org/wiki/Analysis_of_algorithms#Evaluating_run-time_complexity http://en.wikipedia.org/wiki/V8_(JavaScript_engine) Some reading for you. – Lightness Races in Orbit Nov 07 '14 at 12:52
  • @lightness: ok multi-threading is new to me but it doesn't change a thing as parallelism options are the same for both solutions, and performance is still just poor, get slow computer to see the difference – tomasb Nov 07 '14 at 12:57
  • @tomasb: o.O ....... – Lightness Races in Orbit Nov 07 '14 at 13:05
  • ok as you can't get it i made a test for you comparing function above to this function: function checkPallindrome2(num) { var m = Math.floor(num.length/2); for (var i = 0; i < m; i++) { if (num.charAt(i) != num.charAt(num.length-1-i)) return false; } return true; } on my old dualcore for 1 mil cycles the times are 475ms and 31ms!! back to school – tomasb Nov 07 '14 at 13:31
  • @lightness: also only 1 thread is used on this with latest firefox so probably my statement about single-thread container is still right – tomasb Nov 07 '14 at 13:45
  • @tomasb: You're still missing the point entirely. The growth factor in this is negligible (which your naive test does not show), as is a runtime of 475ms for 1 _million_ iterations. Meanwhile, you get increased expressiveness and hugely reduced risk of failure/bugs/errors. So tell me again why a hand-written loop is better? – Lightness Races in Orbit Nov 07 '14 at 13:48
  • @lightness: i stated 'for the case it is used in loop' meaning outer loop for the function of course if this is run just once the time is meaningless; seriously this is very simple example and my opinion is to learn the basics instead of using blocks; such advice will increase numbers of stupid code monkeys and anyway the expressive value of splitting string by "" is not as great as you think – tomasb Nov 07 '14 at 13:54
  • @tomasb: You didn't address a single point in my comment. I have to admit, though, you're right about `.split("")`. – Lightness Races in Orbit Nov 07 '14 at 13:57
  • @LightnessRacesinOrbit: ok growth factor, i assume you talk about places to make a mistake at and not computing time (haha sorry), if you prevent somebody from thinking you do not do a good job, if you are an employer who needs cheap solution using cheap resources you may appreciate such solution like people in microsoft probably do, but that is up to needs or personal preference, i just asked why do you prefer this solution and you came up with performance and expressiveness and algorithms which on such line is none visible, thank you for following me so far – tomasb Nov 07 '14 at 14:07