Skip to content

Commit 78fc8ae

Browse files
authored
fix: membuat kode lebih simpel dan mudah dibaca (#383)
* fix: membuat kode lebih simpel dan mudah dibaca Signed-off-by: slowy07 <slowy.arfy@gmail.com> * fix: memperbaiki operator compare Signed-off-by: slowy07 <slowy.arfy@gmail.com> --------- Signed-off-by: slowy07 <slowy.arfy@gmail.com>
1 parent c2aba28 commit 78fc8ae

File tree

13 files changed

+166
-157
lines changed

13 files changed

+166
-157
lines changed
Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,28 @@
11
<!DOCTYPE html>
22
<html lang="en">
3-
4-
<head>
5-
<meta charset="UTF-8">
6-
<meta http-equiv="X-UA-Compatible" content="IE=edge">
7-
<meta name="viewport" content="width=device-width, initial-scale=1.0">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
87
<title>Realtime Window Size Detection</title>
9-
</head>
8+
</head>
109

11-
<body>
10+
<body>
1211
<h1 id="windowSize"></h1>
13-
</body>
12+
</body>
1413

15-
<script>
14+
<script>
1615
let width = window.innerWidth;
1716
let height = window.innerHeight;
1817

1918
document.getElementById("windowSize").textContent = `${width} * ${height}`;
2019

21-
window.addEventListener("resize", function(){
22-
width = window.innerWidth;
23-
height = window.innerHeight;
24-
document.getElementById("windowSize").textContent = `${width} * ${height}`;
20+
window.addEventListener("resize", function () {
21+
width = window.innerWidth;
22+
height = window.innerHeight;
23+
document.getElementById(
24+
"windowSize"
25+
).textContent = `${width} * ${height}`;
2526
});
26-
</script>
27-
27+
</script>
2828
</html>
Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,23 @@
11
<!DOCTYPE html>
22
<html lang="en">
3-
4-
<head>
5-
<meta charset="UTF-8">
6-
<meta http-equiv="X-UA-Compatible" content="IE=edge">
7-
<meta name="viewport" content="width=device-width, initial-scale=1.0">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
87
<title>Count total words and characters</title>
9-
</head>
8+
</head>
109

11-
<body>
12-
<textarea name="strings" id="strings" cols="50" rows="20" style="font-size: 20px;"></textarea>
10+
<body>
11+
<textarea
12+
name="strings"
13+
id="strings"
14+
cols="50"
15+
rows="20"
16+
style="font-size: 20px"
17+
></textarea>
1318
<h2 id="totalOfWords"></h2>
1419
<h2 id="totalOfChars"></h2>
15-
</body>
16-
17-
<script src="counter.js"></script>
20+
</body>
1821

19-
</html>
22+
<script src="counter.js"></script>
23+
</html>
Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
11
<!DOCTYPE html>
22
<html lang="en">
3-
4-
<head>
5-
<meta charset="UTF-8">
6-
<meta http-equiv="X-UA-Compatible" content="IE=edge">
7-
<meta name="viewport" content="width=device-width, initial-scale=1.0">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
87
<title>Disable Right CLick</title>
9-
</head>
8+
</head>
109

11-
<body>
10+
<body>
1211
<h1>Klik kanan menggunakan mouse anda di manapun di halaman ini.</h1>
13-
</body>
14-
15-
<script src="prevent.js"></script>
12+
</body>
1613

17-
</html>
14+
<script src="prevent.js"></script>
15+
</html>
Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,28 @@
11
<!DOCTYPE html>
22
<html lang="en">
3-
4-
<head>
5-
<meta charset="UTF-8">
6-
<meta http-equiv="X-UA-Compatible" content="IE=edge">
7-
<meta name="viewport" content="width=device-width, initial-scale=1.0">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
87
<title>Prevent Copy</title>
9-
</head>
8+
</head>
109

11-
<body>
10+
<body>
1211
<h1>Copy/salin tulisan apapun yang ada di halaman ini</h1>
13-
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Vero ducimus hic rem ipsam. Ex omnis vel debitis natus
14-
repellat incidunt deleniti repudiandae, aspernatur voluptatibus vero modi nemo ipsam aliquid sequi? Numquam sit
15-
quidem soluta, explicabo veritatis architecto molestias fuga? Dicta laboriosam atque culpa ullam quam pariatur
16-
delectus cupiditate consequuntur necessitatibus numquam velit, veritatis at nemo sed eveniet suscipit temporibus
17-
asperiores ipsum fugiat hic! Quisquam, facilis enim saepe, doloremque ut aperiam quia reiciendis blanditiis nisi
18-
quas perferendis deleniti assumenda vero molestias? Minima aut quas beatae pariatur, quae commodi praesentium
19-
ducimus soluta! Doloremque impedit voluptatem, recusandae quibusdam perferendis quos sed rem excepturi?</p>
20-
</body>
21-
22-
<script src="prevent.js"></script>
12+
<p>
13+
Lorem ipsum dolor sit amet consectetur adipisicing elit. Vero ducimus hic
14+
rem ipsam. Ex omnis vel debitis natus repellat incidunt deleniti
15+
repudiandae, aspernatur voluptatibus vero modi nemo ipsam aliquid sequi?
16+
Numquam sit quidem soluta, explicabo veritatis architecto molestias fuga?
17+
Dicta laboriosam atque culpa ullam quam pariatur delectus cupiditate
18+
consequuntur necessitatibus numquam velit, veritatis at nemo sed eveniet
19+
suscipit temporibus asperiores ipsum fugiat hic! Quisquam, facilis enim
20+
saepe, doloremque ut aperiam quia reiciendis blanditiis nisi quas
21+
perferendis deleniti assumenda vero molestias? Minima aut quas beatae
22+
pariatur, quae commodi praesentium ducimus soluta! Doloremque impedit
23+
voluptatem, recusandae quibusdam perferendis quos sed rem excepturi?
24+
</p>
25+
</body>
2326

24-
</html>
27+
<script src="prevent.js"></script>
28+
</html>
Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,24 @@
11
<!DOCTYPE html>
22
<html lang="en">
3-
4-
<head>
5-
<meta charset="UTF-8">
6-
<meta http-equiv="X-UA-Compatible" content="IE=edge">
7-
<meta name="viewport" content="width=device-width, initial-scale=1.0">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
87
<title>Prevent Paste</title>
9-
</head>
8+
</head>
109

11-
<body>
10+
<body>
1211
<h1>Pastekan nama anda di inputan ini</h1>
1312
<label for="nama">Nama: </label>
14-
<input type="text" name="nama" id="nama">
15-
</body>
13+
<input type="text" name="nama" id="nama" />
14+
</body>
1615

17-
<script>
16+
<script>
1817
const input = document.querySelector("input");
19-
18+
2019
input.addEventListener("paste", function (e) {
21-
e.preventDefault();
22-
alert("Dilarang mem-paste!");
20+
e.preventDefault();
21+
alert("Dilarang mem-paste!");
2322
});
24-
</script>
25-
26-
</html>
23+
</script>
24+
</html>
Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
1+
/**
2+
* konversi string biner ke desimal
3+
* @param {string} binaryString
4+
* @returns {number} return menjadi angka integer
5+
*/
6+
17
const BinaryToDecimal = (binaryString) => {
28
let decimalNumber = 0;
39
const BinaryDigits = binaryString.split("").reverse();
4-
BinaryDigits.forEach((BinaryDigits, index) => {
5-
decimalNumber += BinaryDigits * Math.pow(2, index);
10+
BinaryDigits.forEach((digit, index) => {
11+
decimalNumber += parseInt(digit) * Math.pow(2, index);
612
});
713
console.log(`decimal of ${binaryString} is ${decimalNumber}`);
814
return decimalNumber;
@@ -11,4 +17,11 @@ const BinaryToDecimal = (binaryString) => {
1117
(() => {
1218
BinaryToDecimal("1111111001");
1319
BinaryToDecimal("101010101");
20+
21+
const biner1 = "1111111001";
22+
const biner2 = "101010101";
23+
const desimal1 = BinaryToDecimal(biner1);
24+
const desimal2 = BinaryToDecimal(biner2);
25+
console.log(desimal1 === 1001);
26+
console.log(desimal2 === 341);
1427
})();

algorithm/conversion/decimalToHex.js

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,14 @@
11
// konversi dari desimal ke hexedesimal
22
function intToHex(num) {
3-
switch (num) {
4-
case 10:
5-
return "A";
6-
case 11:
7-
return "B";
8-
case 12:
9-
return "C";
10-
case 13:
11-
return "D";
12-
case 14:
13-
return "E";
14-
case 15:
15-
return "F";
16-
}
17-
return num;
3+
const angkaHex = {
4+
10: "A",
5+
11: "B",
6+
12: "C",
7+
13: "D",
8+
14: "E",
9+
15: "F"
10+
};
11+
return angkaHex[num] || num;
1812
}
1913

2014
function decimalToHex(num) {

algorithm/math/factorTree.js

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,37 +5,37 @@
55
*
66
* @param number = bilangan yang akan dicari faktorialnya
77
*/
8-
function factorTree(number) {
9-
let array = [];
10-
let s = 6;
11-
12-
while (number > 1 && number % 2 === 0) {
13-
number /= 2;
14-
array.push(2);
15-
}
8+
function factorTree(number) {
9+
let array = [];
10+
let s = 6;
11+
12+
while (number > 1 && number % 2 === 0) {
13+
number /= 2;
14+
array.push(2);
15+
}
16+
17+
while (number > 2 && number % 3 === 0) {
18+
number /= 3;
19+
array.push(3);
20+
}
1621

17-
while (number > 2 && number % 3 === 0) {
18-
number /= 3;
19-
array.push(3);
22+
while (number > 4) {
23+
let p = s - 1;
24+
let q = s + 1;
25+
while (number > 4 && number % p === 0) {
26+
number /= p;
27+
array.push(p);
2028
}
2129

22-
while (number > 4) {
23-
let p = s - 1;
24-
let q = s + 1;
25-
while (number > 4 && number % p === 0) {
26-
number /= p;
27-
array.push(p);
28-
}
29-
30-
while (number > 4 && number % q === 0) {
31-
number /= q;
32-
array.push(q);
33-
}
34-
s += 6;
30+
while (number > 4 && number % q === 0) {
31+
number /= q;
32+
array.push(q);
3533
}
36-
37-
return array;
34+
s += 6;
35+
}
36+
37+
return array;
3838
}
39-
39+
4040
console.log(factorTree(15)); // 3,5
41-
console.log(factorTree(18)); // 2,3,3
41+
console.log(factorTree(18)); // 2,3,3

algorithm/other/nameShortener.js

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ function nameShortner(fullname, maxLen = 24, capitalize = 1) {
1616
while (shorten.length >= 2) {
1717
shorten[0].trim();
1818
if (nameLen(shorten) <= maxLen) {
19-
// hilangkan semua singkatan hingga karakter nya sesuai
20-
break;
21-
}
19+
// hilangkan semua singkatan hingga karakter nya sesuai
20+
break;
21+
}
2222
shorten.shift();
2323
}
2424
}
@@ -29,25 +29,24 @@ function nameShortner(fullname, maxLen = 24, capitalize = 1) {
2929
for (const [i, name] of shorten.entries()) {
3030
// i sebagai index, dan name sebagai nama
3131
const currentLen = nameLen(shorten); // memperbarui total panjang nama
32-
32+
3333
if (capitalize) {
34-
// bila nama butuh huruf kapital di awal tiap nama
35-
shorten[i] = capitalized(name);
34+
// bila nama butuh huruf kapital di awal tiap nama
35+
shorten[i] = capitalized(name);
3636
}
3737
if (currentLen <= maxLen && name.length > 2) {
38-
// lewati 1 loop bila total panjang sudah sesuai dan nama belum di singkat
39-
continue;
38+
// lewati 1 loop bila total panjang sudah sesuai dan nama belum di singkat
39+
continue;
4040
}
41-
41+
4242
if (i !== shorten.length - 1) {
4343
// bila nama yang belum di singkat masih lebih dari 1
4444
shorten[i] = name.substring(0, 1).toUpperCase() + "."; // ambil karakter pertama, lalu di buat menjadi kapital, dan tambah kan .
45-
}
46-
else {
45+
} else {
4746
singleName(shorten);
4847
}
4948
}
50-
49+
5150
return shorten.reverse().join(" "); // reverse array nya ke posisi semula dan gabungkan
5251
}
5352

0 commit comments

Comments
 (0)