473,516 Members | 3,456 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Test a number is a 5 digit palindrome using arrays

2 New Member
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 5983
mrhoo
428 Contributor
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 New Member
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 Contributor
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
32521
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 the English term for this process; itemize? tokenize? digitize? sequence?) Some examples:
5
6552
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 false when it should (document.my_form.my_field.value.substring(0) == / \d /) TIA
9
3549
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
41775
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
27285
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 so forth. For example, if the number entered by the user is 4321 then, int array = 1 int array = 2 int array = 3 int...
3
5991
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. Example: invoice number = 700123 invoice check number 700123(checknumber) that check number should make the entire invoice number able to...
20
13365
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
4125
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. Your program should ignore blanks, punctuation, and lettercase Your program should output what the user entered as well as the reversed line. ...
1
3596
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 via an email function. But just can't figure it out. <? //////////////////////////////////////////////////////////// //
0
7405
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7574
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7547
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5106
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4769
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3265
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
1620
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
823
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
487
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.