0

I borrowed a piece of code for reversing a string in JavaScript. However it can't be done.

                <!DOCTYPE HTML>
            <html>
            <head>
            <meta charset="utf-8">
            <title>P4--Largest palindrome product</title>
            </head>
            <body>
            <!--A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99.

            Find the largest palindrome made from the product of two 3-digit numbers.
             --> 
            <script type="text/javascript">
            var newdiv = document.createElement('div');
            newdiv.innerHTML=Palindromic();
                alert(Palindromic());
            function Palindromic(){
                var x;
                var a;
                var b;
                for(i=999*999;i>=100*100;i--)
                {
                    x=i.toString();
                    if(x != x.reverse())
                       continue;
                    else
                    {
                        for(j=100;j<999;j++)
                        {
                            if(i%j == 0)
                            {
                                var y =i/j;
                                if(y>=100 && y<=999)
                                {
                                    a=i;b=y;
                                    break;
                                }
                            }
                        }
                    }
                }
                return x;
            }
            String.prototype.reverse = function() {
              return this.split('').reverse().join('');
            }
            </script>
            </body>
            </html>

The error is "has no method 'reverse'".

3 Answers3

3

You need to define the String.prototype.reverse function before you use it.

Put this code

String.prototype.reverse = function() {
  return this.split('').reverse().join('');
}

at the top instead of at the bottom.

Peter Olson
  • 139,199
  • 49
  • 202
  • 242
  • Yes, you are right. One more question if I could get the answer, it crashes the Firefox because timeout. Any improvement for the algorithm? –  Jan 28 '14 at 16:58
  • @Love It looks like you are trying to solve Problem 4 from [Project Euler](http://projecteuler.net/problem=4). I don't really want to give away a solution algorithm here on Stack Overflow, but if you want solutions, searching for "JavaScript Project Euler problem 4" yields plenty of results. – Peter Olson Jan 28 '14 at 17:04
1

ES6: try this function

function reverse(str) {
           // 1 //    // 2 //   // 3 //
    return [...str].reverse().reduce((rev,el) => rev + el,'');
}
  1. spreads the string into an array of chars
  2. reverses the array
  3. reduce method on array to get the previous value and accumulate.
Durja
  • 637
  • 13
  • 20
  • Can you explain a bit more about what this does? You are using quite a few obscure syntaxes for just one line. It isn't obvious to me how it helps with emojis or weird characters. – Stephen Ostermiller Dec 04 '17 at 22:11
  • Thanks Stephen. Added some helpers. Please let me know if you've got it and if you did - you can up vote. Regarding my warning on split(), it's from my personal experience where I had issues. I don't have the code handy but the above code uses ES6 syntax to reverse a string. – Durja Dec 04 '17 at 23:03
0

Here is the easiest approach:

// reverse a string
function reverseString(str) {
  return str.split('').reverse().join('');
}

const reversedString = reverseString('hello world');
console.log(reversedString); // this would be logged as 'dlrow olleh'
Nechar Joshi
  • 630
  • 1
  • 7
  • 14