Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

159
Views
Adding spaces between the string in js

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

about 4 years ago ยท Juan Pablo Isaza
3 answers
Answer question

0

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.

about 4 years ago ยท Juan Pablo Isaza Report

0

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

about 4 years ago ยท Juan Pablo Isaza Report

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");
about 4 years ago ยท Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
ยฉ 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!