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'".

5
  • 2
    Maybe you should define it before you use it. Commented Jan 28, 2014 at 16:53
  • Probably because you're calling the function before you're declaring it.. Commented Jan 28, 2014 at 16:54
  • Note: It is a bad practice to modify native definitions. Commented Jan 28, 2014 at 16:57
  • @Florent That is a controversial matter. Commented Jan 28, 2014 at 16:58
  • 1
    Note: It is a bad practice to go around saying "X is a bad practice" just cause you read it somewhere. Commented Jan 28, 2014 at 17:03

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Yes, you are right. One more question if I could get the answer, it crashes the Firefox because timeout. Any improvement for the algorithm?
@Love It looks like you are trying to solve Problem 4 from Project Euler. 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.
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.

2 Comments

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.
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.
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'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.