1

How do I insert a hyphen after every 3 characters in a string? Here's what I've tried:

var str = prompt().split("").join(""); // i used split() to convert string to array and join() to remove (,)
var i = 0;
var l = 2;
while (i < str.length) {
  str[l] = "-"; // i think it should puts - every three character but it doesn't 
  l += 3;// i think it should puts - every three character but it doesn't
  i += 1;
};
alert(str);
GirkovArpa
  • 4,427
  • 4
  • 14
  • 43
Alpha Mike
  • 15
  • 5
  • Can you elaborate on your issue? – Rohit Nair Jul 26 '20 at 04:22
  • I think they want to add a hyphen between every 3 characters in a string. – GirkovArpa Jul 26 '20 at 04:24
  • i was expect to put (-) every three letters * just like when you enter a psn card* for example : i enter " 123456789" the output should be : "123-456-789" – Alpha Mike Jul 26 '20 at 04:25
  • To begin with, ```split()``` will convert the string to an array of chars, the ```join("")``` DOES NOT remove the ```(,)``` if you apply these methods on a string, let's say - ``"Hello, world"`` the result will still be ``"Hello, world"`` Can you elaborate on the issue and also provide us with a demo string and the intended result? – Rohit Nair Jul 26 '20 at 04:27

4 Answers4

1

To insert a - (hyphen) between every 3rd character in a string inputted via prompt, use .replace with the following RegExp:

/.{3}(?!$)/g

This matches every sequence of 3 characters, except for the last. The ?! means, unless followed by ... and $ means the end of the string.

If you replace these triple-character sequences with this:

'$&-'

It will effectively insert a hyphen after, since $& is a placeholder for whatever you previously matched.

alert(prompt().replace(/.{3}(?!$)/g, '$&-'));
GirkovArpa
  • 4,427
  • 4
  • 14
  • 43
  • It also adds an '-' after the last digit. – Rohit Nair Jul 26 '20 at 04:35
  • Fixed. Instead of writing `[solved]` you can click the green checkmark on the left of my answer to award me `Best Answer` :) – GirkovArpa Jul 26 '20 at 04:40
  • How can this be modified to insert a hyphen only ONCE. Insert it after the first three digits and that's it. So the end result is something this format: 123-12345 – iChido Feb 21 '23 at 18:08
0

If you want to achieve with a loop:

const str = '123456789';
let result = "";
let counter = 0;

for(i = 0; i < str.length; i++){
    counter++;
    result += str[i];
    if(counter == 3 && str[i] != str.length){
        result += '-';
        counter = 0;
    }
}

console.log(result);
sonEtLumiere
  • 4,461
  • 3
  • 8
  • 35
  • thanks bro it worked , but can you explain why my code didn't please ? – Alpha Mike Jul 26 '20 at 05:10
  • 1
    You are trying to access the index of a string and insert the hypen, but strings are inmutable. https://stackoverflow.com/questions/51185/are-javascript-strings-immutable-do-i-need-a-string-builder-in-javascript/4717855 – sonEtLumiere Jul 26 '20 at 05:15
0

join() function converts array to string, so str[3] doesn't work You can use prompt().split("") instead of prompt().split("").join("");

Please look at this code. This will work

var str = prompt().split("") ; 
var newstr = "";
var i = 0;

while ( i < str.length){
   newstr +=str[i];
   i += 1;
   if(i % 3 ==0 && i !== str.length)
    newstr += "-";
   
};
alert(newstr); 
0

You can use this

var str = prompt().split("") ; 
var newstr = "";
var i = 0;

while ( i < str.length){
   newstr +=str[i];
   i += 1;
   if(i % 3 ==0 && i !== str.length)
    newstr += "-";
   
};
alert(newstr);