473,387 Members | 1,536 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Test a number is a 5 digit palindrome using arrays

2
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:

Expand|Select|Wrap|Line Numbers
  1. var numa
  2. var pal = new Array(5)
  3.  
  4. numa = window.prompt( "Please enter a five digit number number");
  5. num1 = parseInt ( numa );
  6.  
  7.   while (numa < 10000 || numa > 99999 )
  8.   {
  9.    window.alert ("The number is not 5 digits please enter a 5 digit number ");
  10.    numa = window.prompt( "Please enter a five digit number number");
  11.    }
  12.  
  13.    for (var i = 0; i < 5; ++i)
  14.     {pal[ num1 ] = numa;
  15.     }
  16.  
  17.     pal.test (compareIntegers );
  18.  
  19.     function compareIntegers(vaule1, vaule2, vaule4, vaule5)
  20.     {
  21.     if (vaule1 == vaule5 );
  22.       {
  23.       if (vaule2 == vaule4 )
  24.         {
  25.         doucment.writeln ( "The number is a palindrome")
  26.         }
  27.        }
  28.      else   
  29.         document.writeln ("The number is not a palindrome")
  30.         }
  31.  
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.
Feb 12 '08 #1
3 5967
mrhoo
428 256MB
You don't need an array.
You can use a regular expression.


Expand|Select|Wrap|Line Numbers
  1. var n= true,s=s1= 'Enter a 5 digit number';
  2. while(n!= undefined){
  3.     n= prompt(s,'')
  4.     var testpal= /^(\d)(\d)\d\2\1$/.test(n)? '' : ' not ';
  5.     s= n+' is '+testpal+'a 5 digit palindome\n'+s1;
  6. }
Feb 13 '08 #2
hl2ob
2
You don't need an array.
You can use a regular expression.


Expand|Select|Wrap|Line Numbers
  1. var n= true,s=s1= 'Enter a 5 digit number';
  2. while(n!= undefined){
  3.     n= prompt(s,'')
  4.     var testpal= /^(\d)(\d)\d\2\1$/.test(n)? '' : ' not ';
  5.     s= n+' is '+testpal+'a 5 digit palindome\n'+s1;
  6. }

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.
Feb 13 '08 #3
mrhoo
428 256MB
Try the code with anything other than a 5 digit number in the prompt
Feb 13 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: Rune Strand | last post by:
Hi, If I have a lot of integers and want do something with each digit as integer, what is the fastest way to get there? Eg. Make 12345 into an iterable object, like or "12345" (Btw: What is...
5
by: Dave Miller | last post by:
Hello - I have a form field the results from which should not start with a digit. I have tried various permutations of the following without success. Can anyone help me out? //does not return...
9
by: David | last post by:
Hi all, I have an array of strings. For example... = "entry1 first entry"; = "entry2 second entry"; = "entry3 third entry"; etc... How can I sort this array by the number after "entry"?
13
by: Hako | last post by:
I try this command: >>> import string >>> string.atoi('78',16) 120 this is 120 not 4E. Someone can tell me how to convert a decimal number to hex number? Can print A, B, C,DEF. Thank you.
2
by: hikmaz | last post by:
I am trying to get the rightmost digits (%10) of a number (taken from the user) and store it into successive array locations and get rid of the rightmost digit (\10) to store the next and so on and...
3
by: Kris | last post by:
I need to create a valid mod10 or luhn number that can be attached to an invoice, I have found several examples showing how to validate a credit card number or validate a number using mod10/luhn. ...
20
by: Wabz | last post by:
Hello mates, Does anyone know how to write a function that tests if an integer is a palindrome in C language?
2
by: bigtd08 | last post by:
help writing this palindrome program for my c++ class. HERE WHAT THE CODE SHOULD BE LIKE. Write a program that takes a line of input from the keyboard and check to see if that line is a palindrome....
1
by: jlt206 | last post by:
This code <?php include("counter.php")?> on the webpage produces the count number. (function code below) I want to place the current number into a variable $MemberNo or into a FormField to be sent...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.