-2

Hello there once again,

i generate a string of 6 digits with the code below as such =>

var chars = "ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
            var string_length = 6;
            var randomstring = '';

            for (var x = 0; x < string_length; x++) {

                var letterOrNumber = Math.floor(Math.random() * 2);
                if (letterOrNumber == 0) {
                    var newNum = Math.floor(Math.random() * 9);
                    randomstring += newNum;
                } else {
                    var rnum = Math.floor(Math.random() * chars.length);
                    randomstring += chars.substring(rnum, rnum + 1);
                }

            }
            const string = randomstring;

But i actually wanna add spaces between the string like

if the string is bc48snwk2 spaces should be added to make the string look as such -> b c 4 8 s n w k 2

1

3 Answers 3

1

You can use string.split("").join(" "), like this:

var chars = "ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
var string_length = 6;
var randomstring = "";

for (var x = 0; x < string_length; x++) {
  var letterOrNumber = Math.floor(Math.random() * 2);
  if (letterOrNumber == 0) {
    var newNum = Math.floor(Math.random() * 9);
    randomstring += newNum;
  } else {
    var rnum = Math.floor(Math.random() * chars.length);
    randomstring += chars.substring(rnum, rnum + 1);
  }
}
const string = randomstring;
console.log(string.split("").join(" "));

This code (string.split("").join(" ")) will first split the string (ex: v0Q75P) into an array like this ["v", "0", "Q", "7", "5", "P"] and then join it with a space between the elements like this v 0 Q 7 5 P.

console.log(string); // v0Q75P
console.log(string.split("")); // ["v", "0", "Q", "7", "5", "P"]
console.log(string.split("").join(" ")); // v 0 Q 7 5 P


Some useful resources:

👉 String.prototype.split() Documentation

👉 Array.prototype.join() Documentation

Sign up to request clarification or add additional context in comments.

Comments

1

Here is how you could achieve this:

const chars = "ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
const length = 6;

const randomInteger = (max) => Math.floor(Math.random() * max);

const randomNumberOrLetter = (chars) => () =>
  randomInteger(2) ? randomInteger(10) : chars[randomInteger(chars.length)];

const result = Array.from({ length }, randomNumberOrLetter(chars)).join(" ");

console.log(result);

In my answer I'm assuming that you need digits between 0 and 9 (not 0 and 8) and that your Math.floor(Math.random() * 9) line is a mistake.

Comments

0

You have several options for achieving that.

Turn to Array & Join:

var newString = Array.from(randomString).join(" ");

This will make the string an array and then turn it back to a string with an extra space between each character.

Replace with RegEx:

var newString = randomString[0] + randomString.slice(1).replace(/(\w)/g, " $1");

// Or a more complex regex
var newString = randomString.replace(/\w(\w)/g, " $1");

Comments