Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
so i have
Input : "Dlasdf234dkl sdfkl8886adaf15adfjk7 asdflkj376661a s445s198"

and need to get this -
Output : "Dlasdf432dkl sdfkl6888adaf51adfjk7 asdflkj166673a s544s891"


as u see only numbers are reversed, and im not sure how to isolate numbers from it, and reverse only them without toucnhing letters..probably easy,but my js is little bit rusty,i can't find solution online...i found for intiger only but not together..

What I have tried:

<script>
	function myfunction(a){
   var x=a.toString();
   var y= x.split(" ");
   var z=y.reverse();
   var result=z.join("");
     return result;
}

 myfunction( "Dlasdf234dkl sdfkl8886adaf15adfjk7 asdflkj376661a s445s198");


</script>
Posted
Updated 3-Oct-17 1:28am

There is nothing standard that will do that.
You will have to process the string, find the start and end of the integer by identifying digit and non-digit characters, then swap ends:

start:       123456
first swap:  623451
second swap: 653421
third swap:  654321
 
Share this answer
 
Comments
Kornfeld Eliyahu Peter 1-Oct-17 10:42am    
A RegEx can help to split the string and identify the runs...
OriginalGriff 1-Oct-17 10:47am    
Yes - won't reverse them though! :laugh:
My JavaScript is pretty ... ahem, basic.... but I enjoy getting there in the end! :)

Here's a simple HTML page that does it - no doubt those who know about these things ca do it far better, but I hope this helps you learn something...

<html>
  <head>
    <script type="text/javascript">
        function isNumeric(n) {
            return !isNaN(parseInt(n)) && isFinite(n);
        }
        function solveMe() {
            var sOut = ''   // string to hold final answer
            var sIn = document.getElementById("tIN").value;
            if (sIn.length > 0) {
                var n = [''];  // array to holder integers
                var a = [''];  // array to hold other characters
                var i = 0;
                var k; 
                var t;
                i = 0;
                while (i < sIn.length) {
                    if (isNumeric(sIn.charAt(i))) {
                         // if character at position i is numeric add it to array "n" at position i
                         // and a blank string to array "a" at position i
                        n.push(sIn.charAt(i));
                        a.push('');
                    } else {
                        // add to opposite arrays if it's not numeric
                        a.push(sIn.charAt(i));
                        n.push('');
                    }
                    i++;
                }
                i = 1;  // skip first eleemtn of array (it's ewmpty from initialisation))
                while (i < a.length) {
                    if (a[i] == '') {
                        // if element i in array "a" is empty, the correspsonding character in the "n" array will be a number
                        // so, first make a temp string of it and any successive numbers
                        k = i;
                        t = '';
                        while ((k < a.length) && (a[k] == '')) {
                            t += n[k];
                            k++;
                            i++;  // remember to increment i as well!
                        }
                        i--;  // and decrement by 1 at the end becasue we'll increment it again at end of while koop
                        // now add the characters in the temp string in reverse order to the answer string
                        k = t.length-1;
                        while (k > -1) {
                            sOut += t.charAt(k);
                            k--
                        }
                    } else {
                        // if element i in array "a" is a non-numeric character, simply add it to the answer string
                        sOut += a[i];
                    }
                    i++;
                }
            }
            document.getElementById("dX").innerHTML = sOut;
        }
    </script>
  </head>
  <body>
   <p><input type="text" id="tIN" style="width:500px" value="Dlasdf234dkl sdfkl8886adaf15adfjk7 asdflkj376661a s445s198" /></p>
      <p><a href="#" onclick="solveMe()">Solve</a></p>
      <div id="dX"></div>
  </body>
</html>
 
Share this answer
 
Comments
A_Griffin 1-Oct-17 11:43am    
PS - NB I have refrained from even asking WHY you would ever want to do such a thing......
Sfjklm 1-Oct-17 12:12pm    
This code doesn't work..
A_Griffin 1-Oct-17 13:55pm    
Well it's copy/pasted exactly from a page that worked for me. What's wrong when you try it?
Sfjklm 1-Oct-17 14:00pm    
Uncaught SyntaxError: Unexpected token <
Uncaught ReferenceError: solveMe is not defined
at HTMLAnchorElement.onclick (zadat.html:62)
A_Griffin 1-Oct-17 14:07pm    
Well you haven't copy/pasted right then - because you can see for yourself that solveMe is defined. Try copy/pasting the entirety f my code and saving it as is as an HTML file and running it. Then worry about porting it into your code.
Here's a much shorter version - I was going round the houses unnecessarily above....
<html>
  <head>
    <script type="text/javascript">
        function isNumeric(n) {
            return !isNaN(parseInt(n)) && isFinite(n);
        }
        function solveMe() {
            var sOut = ''   // string to hold final answer
            var sIn = document.getElementById("tIN").value;
            if (sIn.length > 0) {
                var i = 0;
                var k; 
                var t;
                i = 0;
                while (i < sIn.length) {
                    if (isNumeric(sIn.charAt(i))) {
                        // if char is a number, first make a temp string of it and any successive numbers
                        k = i;
                        t = '';
                        while ((k < sIn.length) && (isNumeric(sIn.charAt(k)))) {
                            t += sIn.charAt(k);
                            k++;
                            i++;  // remember to increment i as well!
                        }
                        i--;  // and decrement by 1 at the end becasue we'll increment it again at end of while koop
                        // now add the characters in the temp string in reverse order to the answer string
                        k = t.length - 1;
                        while (k > -1) {
                            sOut += t.charAt(k);
                            k--
                        }                      
                    } else {
                        // add to opposite arrays if it's not numeric
                        sOut += sIn.charAt(i);
                    }
                    i++;
                }
            }
            document.getElementById("dX").innerHTML = sOut;
        }
    </script>
  </head>
  <body>
   <p><input type="text" id="tIN" style="width:500px" value="Dlasdf234dkl sdfkl8886adaf15adfjk7 asdflkj376661a s445s198" /></p>
      <p><a href="#" onclick="solveMe()">Solve</a></p>
      <div id="dX"></div>
  </body>
</html>
 
Share this answer
 
v3
Comments
A_Griffin 1-Oct-17 14:18pm    
.. and this DOES work! (As does the original...)
Sfjklm 1-Oct-17 14:29pm    
i swear to god...i get =
Uncaught ReferenceError: solveMe is not defined
at HTMLAnchorElement.onclick (VM87 zadat.html:46)
A_Griffin 1-Oct-17 14:40pm    
Then you're doing something else wrong - because HTMLAnchorElement.onclick (VM87 zadat.html:46) is not part of my code, is it?
Sfjklm 1-Oct-17 14:48pm    
yea,sry ..my bad...
A_Griffin 1-Oct-17 14:46pm    
btw, just fixed a typo at the line
sOut += sIn.charAt(i);
It had an extra ) in before.
Quote:
i can't find solution online

Just Googling on internet is not your job, as programmer, your job is to create algorithms.
The algorithm great lines are:
- Scan the input for sequences of digits
- For each sequence, replace it with the reverse

To help you analyze problems, you should learn one or more analyze methods, E.W. Djikstra top-Down method is a good start.
https://en.wikipedia.org/wiki/Top-down_and_bottom-up_design[^]
https://en.wikipedia.org/wiki/Structured_programming[^]
https://en.wikipedia.org/wiki/Edsger_W._Dijkstra[^]
https://www.cs.utexas.edu/users/EWD/ewd03xx/EWD316.PDF[^]

For this problem, RegEx (Regular Expression) can greatly simplify the code. RegEx are used to match sequences in strings and to do things with matches like replacing them in string.
JavaScript RegExp Object[^]
Just a few interesting links to help building and debugging RegEx.
Here is a link to RegEx documentation:
perlre - perldoc.perl.org[^]
Here is links to tools to help build RegEx and debug them:
.NET Regex Tester - Regex Storm[^]
Expresso Regular Expression Tool[^]
RegExr: Learn, Build, & Test RegEx[^]
This one show you the RegEx as a nice graph which is really helpful to understand what is doing a RegEx:
Debuggex: Online visual regex tester. JavaScript, Python, and PCRE.[^]


Note: This code is over complicated:
JavaScript
function myfunction(a){
   var x=a.toString();
   var y= x.split(" ");

you convert a to string, but it is already a string, so this code can be simplified to:
JavaScript
function myfunction(a){
   var y= a.split(" ");

Here
JavaScript
myfunction( "Dlasdf234dkl sdfkl8886adaf15adfjk7 asdflkj376661a s445s198");

you forgot to do something with the result of myfunction.
JavaScript
result= myfunction( "Dlasdf234dkl sdfkl8886adaf15adfjk7 asdflkj376661a s445s198");
 
Share this answer
 
Actually not that complicated:



String.prototype.reverse = function() {
return this.replace(/[0-9]+/gm, function(item) {
return item.split("").reverse().join("");
});
}

document.writeln("Dlasdf234dkl sdfkl8886adaf15adfjk7 asdflkj376661a s445s198".reverse());
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900