0

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.

Sapphy
  • 29
  • 4
  • 2
    you can use a `switch`-statement instead of repeating `if/else`-statements. Also you can randomize out of an array. – tacoshy May 23 '22 at 17:04
  • 4
    Comparisons are done using `==` or `===` –  May 23 '22 at 17:06
  • 4
    `Math.random()` does not accept a parameter `Math.random(16)` doesn't do anything special, still returns a number in the range [0,1). And if you round it down, you always get a zero. – VLAZ May 23 '22 at 17:08
  • 1
    You need this: `Math.floor(Math.random() * (1 << 24)).toString(16).padStart(6, '0')` –  May 23 '22 at 17:11
  • @ChrisG Ok, but where? – Sapphy May 23 '22 at 17:24
  • Seeing as it [doesn't appear to be bound to just hexadecimal characters](https://prnt.sc/qwerty), I would think you could just [utilize this](https://stackoverflow.com/a/1349426/231316), adjusting the alphabet for no upper case – Chris Haas May 23 '22 at 17:26
  • You might want to consider using a JSON that way instead of checking the value of a, you can just look up the value of a then if it is undefined, then you know it's not in the JSON and add the number instead. This also makes it easier to add more options – Charlie Ciampa May 23 '22 at 17:37
  • This is a dupe: https://stackoverflow.com/questions/58325771/how-to-generate-random-hex-string-in-javascript –  May 24 '22 at 07:08
  • What I posted is an expression that generates a random six digit hex number. You can assign it to a constant or just add it to the rest of the URL. –  May 24 '22 at 07:09
  • @CharlieCiampa A JSON? What does JSON have to do with generating a random hex string? –  May 24 '22 at 07:10

2 Answers2

1

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>
tacoshy
  • 10,642
  • 5
  • 17
  • 34
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);
}


Manh Do
  • 111
  • 8