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.

10
  • 2
    you can use a switch-statement instead of repeating if/else-statements. Also you can randomize out of an array. Commented May 23, 2022 at 17:04
  • 4
    Comparisons are done using == or === Commented May 23, 2022 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. Commented May 23, 2022 at 17:08
  • 1
    You need this: Math.floor(Math.random() * (1 << 24)).toString(16).padStart(6, '0') Commented May 23, 2022 at 17:11
  • @ChrisG Ok, but where? Commented May 23, 2022 at 17:24

2 Answers 2

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>

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

1 Comment

let hex = Math.floor(Math.random() * 16).toString(16); would seem better....
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);
}


1 Comment

Why is this the accepted answer? It's clearly not correct.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.