36 users online (1 members and 35 guests)  


  Results 1 to 3 of 3

Related

  1. javascript link problem    Forum: Javascript Forum
    Replies: 4
  2. Simple Javascript Problem - I think...    Forum: Javascript Forum
    Replies: 3
  3. Javascript active link problem    Forum: Javascript Forum
    Replies: 0
  4. Help me with a Javascript problem!    Forum: Javascript Forum
    Replies: 10
  5. problem with javascript opening a new window    Forum: Javascript Forum
    Replies: 4
  1. #1
    marylove2007's Avatar
    New User

    Status
    Offline
    Join Date
    Mar 2007
    Posts
    1

    Please Help in a JavaScript Problem

    Help Plzzzz!!!!!!!!!!!!!!!!!!!...
    > A palindrome is a number or a text phrase that reads the same backward and forward. For example, each of the following five-digit integers is a palindrome: 12321, 55555, 45554 and 11611.
    > ,mvcxmzxWrite a script that reads in a five-digit integer and determines whether it is a palindrome. If the number is not five digits long or is not a valid number, output XHTML text that displays an
    > alert dialog indicating the problem to the user. Allow the user to enter a new value after dismissing the alert dialog. Continue the whole process until the user enters “stop”.
    var startNumber = parseInt(12345/10000)
    var endNumber = 12345%10
    var remainNumber = parseIn((12345-startNumber*10000)/10)
    > This is not a Homework at all.
    > plzz in Java script.
    > Hellllllllllllllllllllllllllllp

  2. #2
    DeadMeatGF's Avatar
    Moderator

    Status
    Offline
    Join Date
    Sep 2005
    Posts
    381

    Re: Please Help in a JavaScript Problem

    I've nicked this string reversal function off t'internet:
    Code:
    // Reverse string
    function reverse() {
      var inp = document.rev.inpt.value
      var outp = 0
      for (i = 0; i <= inp.length; i++) {
        outp = inp.charAt (i) + outp
      }
      document.reals.outpt.value = outp
    }
    - all you need to do is test wuth something like
    Code:
    if (strFiveDigits == reverse(strFiveDigits)) { ... }
    I don't do js, so testing for 5 digits etc you'd have to do yourself!

  3. #3
    coothead's Avatar
    bald headed old fart

    Status
    Offline
    Join Date
    Aug 2003
    Location
    chertsey, a small town 25 miles south west of london, england.
    Posts
    732

    Re: Please Help in a JavaScript Problem

    Hi there marylove2007,

    and a warm welcome to these forums.

    Here is an example that works for any length number...
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
       "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
    <title>palindrome tester</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    
    <style type="text/css">
    fieldset {
        width:300px;
        padding:0 20px 20px 20px;
        border:3px double #999;
        margin:auto;
     }
    div {
        margin:20px 0 5px 0;
     }
    </style>
    
    <script type="text/javascript">
      var df=document.forms;
      var temp=0;
    window.onload=function(){
       init();
       df[0][2].onclick=palindromeTest;
       df[0][3].onclick=init;
     }
    
    function init() {
       df[0][1].focus();
       temp=0;
     }
    
    function palindromeTest(){
    if((isNaN(df[0][1].value))||(df[0][1].value=='')){
       alert('please enter a number');
       df[0].reset();
       df[0][1].focus();
       return;
     }
       x=df[0][1].value.split('');
       y=x.reverse();
    for(c=0;c<x.length;c++) {
    if(y[c]==x[x.length-c-1]){
       temp++;
      }
     }
    if(temp==x.length){
       alert('this number is palindromic');
       temp=0;
     }
    else{
       alert('this number is not palindromic');
       temp=0;
      }
     }
    </script>
    
    </head>
    <body>
    
    <form action="#">
    <fieldset>
    <legend>palindrome tester</legend>
    <div>
    <input type="text"/><label> : enter number</label>
    </div>
    <input type="button" value="test"/>
    <input type="reset" value="clear"/>
    </fieldset>
    </form>
    
    </body>
    </html>
    If you definitely only want it to test 5 digit numbers change this ...
    Code:
    if((isNaN(df[0][1].value))||(df[0][1].value=='')){
       alert('please enter a number');
       df[0].reset();
       df[0][1].focus();
       return;
     }
    ...to this...
    Code:
    if((isNaN(df[0][1].value))||(df[0][1].value=='')||(df[0][1].value.length>5)){
       alert('please enter a number no longer than 5 digits');
       df[0].reset();
       df[0][1].focus();
       return;
     }



Tags for this Thread