Blog/Components/Pages/Generator.razor.js 1.2 K · 47 lines · raw · history

1 import { getById, h } from "/common.module.js";
2
3 function writeInfo(label, msg) {
4 log.appendChild(
5 h("div",
6 [h("div", {class: "info"}, [h("span", {class: "noselect"}, label), h("span", msg)])]
7 ),
8 );
9 }
10
11 export function onLoad() {
12 const gen_bsn = getById("bsn");
13 gen_bsn.addEventListener("click", generateBsn);
14 }
15
16 function generateBsn() {
17 const nr9 = Math.floor(Math.random() * 7);
18 const nr8 = Math.floor(Math.random() * 10);
19 const nr7 = Math.floor(Math.random() * 10);
20 const nr6 = Math.floor(Math.random() * 10);
21 const nr5 = Math.floor(Math.random() * 10);
22 const nr4 = Math.floor(Math.random() * 10);
23 const nr3 = Math.floor(Math.random() * 10);
24 let nr2 = Math.floor(Math.random() * 10);
25 const bsnNumber =
26 9 * nr9 +
27 8 * nr8 +
28 7 * nr7 +
29 6 * nr6 +
30 5 * nr5 +
31 4 * nr4 +
32 3 * nr3 +
33 2 * nr2;
34 let nr1 = Math.floor(bsnNumber - Math.floor(bsnNumber / 11) * 11);
35 if (nr1 > 9) {
36 if (nr2 > 0) {
37 nr2 -= 1;
38 nr1 = 8;
39 } else {
40 nr2 += 1;
41 nr1 = 1;
42 }
43 }
44
45 const BSNString = "" + nr9 + nr8 + nr7 + nr6 + nr5 + nr4 + nr3 + nr2 + nr1;
46 writeInfo("BSN: ", BSNString);
47 }