Test a number is a 5 digit palindrome using arrays

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • hl2ob
    New Member
    • Feb 2008
    • 2

    Test a number is a 5 digit palindrome using arrays

    Alright I'm still new to javascript. I was getting it pretty well, and getting everything alright untill this point.

    We have to make a program that test a 5 digit number as a palindrome. I have no clue on how to do this. I was given advice that I'd need to use arrays, but for javascript I have no idea how to work with them. I read some stuff in a book and read some tutorials, but this is the best I can do:

    Code:
    var numa
    var pal = new Array(5)
    
    numa = window.prompt( "Please enter a five digit number number");
    num1 = parseInt ( numa );
    
      while (numa < 10000 || numa > 99999 )
      {
       window.alert ("The number is not 5 digits please enter a 5 digit number ");
       numa = window.prompt( "Please enter a five digit number number");
       }
       
       for (var i = 0; i < 5; ++i)
        {pal[ num1 ] = numa;
        }
        
        pal.test (compareIntegers );
        
        function compareIntegers(vaule1, vaule2, vaule4, vaule5)
        {
        if (vaule1 == vaule5 );
          {
          if (vaule2 == vaule4 )
            {
            doucment.writeln ( "The number is a palindrome")
            }
           }
         else   
            document.writeln ("The number is not a palindrome")
            }
    I know I'm probably way off, as the page is just complety blank, but sadly I really have no clue what to do. The first part (Testing to make sure it's a 5 didgit number) I have down. Other than that, I could use any advice that could be given .
    Thank you for your time and help.
  • mrhoo
    Contributor
    • Jun 2006
    • 428

    #2
    You don't need an array.
    You can use a regular expression.


    Code:
    var n= true,s=s1= 'Enter a 5 digit number';
    while(n!= undefined){
    	n= prompt(s,'')
    	var testpal= /^(\d)(\d)\d\2\1$/.test(n)? '' : ' not ';
    	s= n+' is '+testpal+'a 5 digit palindome\n'+s1;
    }

    Comment

    • hl2ob
      New Member
      • Feb 2008
      • 2

      #3
      Originally posted by mrhoo
      You don't need an array.
      You can use a regular expression.


      Code:
      var n= true,s=s1= 'Enter a 5 digit number';
      while(n!= undefined){
      	n= prompt(s,'')
      	var testpal= /^(\d)(\d)\d\2\1$/.test(n)? '' : ' not ';
      	s= n+' is '+testpal+'a 5 digit palindome\n'+s1;
      }

      Ok that's helpful, but part of the code has to test to make sure it's a 5 digit number. In order to do that would I have to add in n > 99999 II n < 10000 in there somewhere?

      Thanks again.

      Comment

      • mrhoo
        Contributor
        • Jun 2006
        • 428

        #4
        Try the code with anything other than a 5 digit number in the prompt

        Comment

        Working...