Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

161
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda