Inserting into a number string - Javascript String Operation

Javascript examples for String Operation:String Format

Description

Inserting into a number string

Demo Code

ResultView the demo in separate window

<html>
   <head> 
      <meta name="viewport" content="width=device-width, initial-scale=1"> 
      <script type="text/javascript">
    window.onload=function(){//from www  .  jav  a2s .  c  o m
function DashInsert(num) {
  num = num.split("");
  var my = num.slice(0);
  var offset = 0;
  for (i = 1; i < num.length; i++) {
    if (num[i - 1] % 2 != 0 && num[i] % 2 != 0) {
      my.splice(i + offset, 0, "-");
      offset++;
    }
  }
  my = my.join("");
  return my;
}
console.log(DashInsert("45739"));
    }

      </script> 
   </head> 
   <body>  
   </body>
</html>

Related Tutorials