Reverse a string, but only the digits
Question:
Any ideas?
I am trying to set up a way to track applicants that I am working with. What I wanted to do is take the persons phone number and reverse it and make it the applicant number.
Input: 719-964-5776
Output: 6775469917
Answer:
The code is pretty straightforward - view source on this page to grab a version of the code with proper comments:
Source Codefunction reverseNumbersOnly(inString) { tempVar = ''; for (i=inString.length-1;i>-1;i--) { if (inString.charAt(i)==parseInt(inString.charAt(i))) { tempVar+=inString.charAt(i); } }; return tempVar; } |
In Action:Enter phone numbers in the space below: |
Source Code
<script language="JavaScript" type="text/javascript"> <!-- function reverseNumbersOnly(inString) { /* returns the reversed value */ /* temporary storage */ tempVar = ''; /* go the length of the string in reverse */ for (i=inString.length-1;i>-1;i--) { /* check to see if it's a number, we only want numbers */ if (inString.charAt(i)==parseInt(inString.charAt(i))) { /* if it IS a number, we store it for later */ tempVar+=inString.charAt(i); } } return tempVar; } //--> </script>