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:
- How it got 0 6 out of 6 times,
- How it got all A's out of those zeros.
If someone could could fix this, please let me know.
CodePudding user response:
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>
CodePudding user response:
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);
}