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

266
Vistas
(proper) Randomization in JavaScript

So there's this image uploading service called LightShot. You can easily go to any image on there with "prnt.sc/" and a 6-digit sequence of letters & numbers.
I thought it would be cool to program some code that gives you a random link to the site. Here's the code:

function func() {
    let x = 'https://prnt.sc/';
    let a = 1;
    let b = '';

    for (i = 0; i <= 5; i++) {
        a = Math.floor(Math.random(16));
        console.log(a);
        if (a = 10) {
            b += 'a';
        } else if (a = 16) {
            b += 'a';
        } else if (a = 12) {
            b += 'c';
        } else if (a = 11) {
            b += 'b';
        } else if (a = 13) {
            b += 'd';
        } else if (a = 14) {
            b += 'e';
        } else if (a = 15) {
            b += 'f';
        } else { 
            b += a.toString();
        }
    } x += b;
    console.log(x);
}
<!doctype html>

<html>

<head>
    <meta charset="utf-8">
    <script src="./sketch.js"></script>
</head>

<body>
    <button onclick='func()'>Go</button>
</body>

</html>

Meanwhile, in the console...

➏ 0
(insert the rest here)/aaaaaa

Well, maybe it's something to do with the block of IF statements or the toString() function, but I don't know:

  1. How it got 0 6 out of 6 times,
  2. How it got all A's out of those zeros.

If someone could could fix this, please let me know.

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Just get the number or letter from an array as shown below and run the randomizer 6 times.

First you have to define a variable with an array that includes all the possibilies (0, 1, 3 ... 8, 9, A, B, ... E, F):
var random = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'];

Then you can use the random()-function to run a choice out fo that array:
random[Math.floor(Math.random() * random.length)];

document.querySelector('button').addEventListener("click", function() {
  var string = ""
  var random = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'];
  for (i = 0; i <= 5; i++) {
    var number = random[Math.floor(Math.random() * random.length)];   
    string += number;
  }
  console.log(string);
});
<button>Click me</button>

about 4 years ago · Juan Pablo Isaza Denunciar

0

I think, the logical of random is important, you are mistake

you replace blow

a = Math.floor(Math.random(16));

with

a = Math.floor(Math.random() * 10 + 7);

and replace "=" with "==="

total fix

function func() {
  let x = "https://prnt.sc/";
  let a = 1;
  let b = "";

  for (i = 0; i <= 5; i++) {
    a = Math.floor(Math.random() * 10 + 10);
    console.log(a);
    if (a === 10) {
      b += "a";
    } else if (a === 16) {
      b += "a";
    } else if (a === 12) {
      b += "c";
    } else if (a === 11) {
      b += "b";
    } else if (a === 13) {
      b += "d";
    } else if (a === 14) {
      b += "e";
    } else if (a === 15) {
      b += "f";
    } else {
      b += a.toString();
    }
    console.log(b);
  }
  x += b;
  console.log(x);
}


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