Skip to content

Commit 0c14347

Browse files
committed
add problem3
1 parent 26e3056 commit 0c14347

File tree

5 files changed

+141
-87
lines changed

5 files changed

+141
-87
lines changed

problemSolvingPractices/problems/p1.js

Lines changed: 0 additions & 44 deletions
This file was deleted.

problemSolvingPractices/problems/p2.js

Lines changed: 0 additions & 43 deletions
This file was deleted.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<html lang='en'>
4+
<head>
5+
<meta charset='UTF-8'>
6+
<meta name='viewport' content='width=device-width, initial-scale=1.0'>
7+
<title>Binary Conversion</title>
8+
<link rel='stylesheet' href='style.css'>
9+
<script src='p3-BinaryConversion.js' defer></script>
10+
</head>
11+
<body>
12+
<h3> Convert an IPv4 address from Binary to Decimal </h3><br>
13+
<!-- <h3> Example: 10101100.00010000.00100010.00000011(Binary) to 172.16.34.3 </h3><br> -->
14+
15+
<input type="text" placeholder="Example: 10101100.00010000.00100010.00000011" id="input1">
16+
<button id="but1">Convert</button>
17+
<br><br>
18+
<h4 id="result"></h4>
19+
20+
</body>
21+
</html>
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
// Binary to Decimal
2+
//
3+
4+
5+
const Binary = document.getElementById('input1')
6+
const ConvertButton = document.getElementById('but1')
7+
8+
ConvertButton.addEventListener('click', () => {
9+
// .trim() gets rid of spaces at start or end of string
10+
const result = BinaryToDecimal(Binary.value.trim())
11+
12+
document.getElementById('result').innerText = result
13+
})
14+
15+
Binary.addEventListener('keydown', (event) => {
16+
if (event.key === 'Enter') {
17+
18+
const result = BinaryToDecimal(Binary.value.trim())
19+
20+
document.getElementById('result').innerText = result
21+
22+
}
23+
})
24+
25+
26+
27+
const errorMsg1 = 'Please insert an IPv4 address in Binary (4 bytes)'
28+
const errorMsg2 = 'Please make sure the input format is correct and each Octet is exactly 8 numbers'
29+
30+
function BinaryToDecimal(Binary){
31+
32+
if (!Binary) return errorMsg1
33+
if (Binary.length !== 35) return errorMsg1;
34+
35+
const chart = [128,64,32,16,8,4,2,1]
36+
37+
let firstOctetDecimal = 0
38+
let secondOctetDecimal = 0
39+
let thirdOctetDecimal = 0
40+
let fourthOctetDecimal = 0
41+
42+
43+
const octets = Binary.split('.')
44+
45+
if (octets[0].length < 8) return errorMsg2
46+
47+
const firstOctet = octets[0].split('')
48+
49+
for (let i=0; i < chart.length; i++) {
50+
if (firstOctet[i] === '1') {
51+
firstOctetDecimal += chart[i]
52+
}
53+
};
54+
55+
if (octets[1].length < 8) return errorMsg2
56+
57+
const secondOctet = octets[1].split('')
58+
59+
for (let i=0; i < chart.length; i++) {
60+
if (secondOctet[i] === '1') {
61+
secondOctetDecimal += chart[i]
62+
}
63+
};
64+
65+
if (octets[2].length < 8) return errorMsg2
66+
67+
const thirdOctet = octets[2].split('')
68+
69+
for (let i=0; i < chart.length; i++) {
70+
if (thirdOctet[i] === '1') {
71+
thirdOctetDecimal += chart[i]
72+
}
73+
};
74+
75+
if (octets[3].length < 8) return errorMsg2
76+
77+
const fourthOctet = octets[3].split('')
78+
79+
for (let i=0; i < chart.length; i++) {
80+
if (fourthOctet[i] === '1') {
81+
fourthOctetDecimal += chart[i]
82+
}
83+
};
84+
85+
86+
return IPAddressInDecimal = `Result: ${firstOctetDecimal}.${secondOctetDecimal}.${thirdOctetDecimal}.${fourthOctetDecimal}`
87+
88+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
* {
2+
padding: 0;
3+
margin: 0;
4+
font-family: Arial;
5+
}
6+
7+
input:focus {
8+
outline: 1px solid lightslategray;
9+
}
10+
11+
input {
12+
width: 350px;
13+
padding: 2px 2px;
14+
font-weight: bold;
15+
}
16+
17+
button {
18+
background-color: white;
19+
padding: 3px 2px;
20+
border: 1px solid black;
21+
border-radius: 5px;
22+
font-weight: bold;
23+
}
24+
25+
button:hover {
26+
opacity: 0.8;
27+
cursor: pointer;
28+
}
29+
30+
button:active {
31+
opacity: 0.5;
32+
}

0 commit comments

Comments
 (0)