generated from chalu/holiday-challenge-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.mjs
109 lines (89 loc) · 3.1 KB
/
app.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
//====== telecom prefixes ======
const networkThings = {
mtn: ["0803", "0806", "0703", "0706", "0813", "0816", "0810", "0814", "0903"],
airtel: ["0802", "0808", "0708", "0812", "0701", "0902"],
glo: ["0805", "0807", "0705", "0815", "0811", "0905"],
etisalat: ["0809", "0818", "0817", "0909"],
};
//======== telecom logos ========
let mtnLogo = document.querySelector(".mtn");
let airtelLogo = document.querySelector(".airtel");
let gloLogo = document.querySelector(".glo");
let etisalatLogo = document.querySelector(".etisalat");
function startApp() {
//======= getting the user's input value while they type =======
const userInput = document.querySelector("#output");
userInput.addEventListener("input", getUserInput);
function getUserInput() {
const userInputValue = userInput.value;
const userNetwork = findNetwork(userInputValue);
showLogo(userNetwork);
}
// ====== A function comparing user's input with prefixes in the object(networkThings)======
function findNetwork(phoneNum) {
// console.log(phoneNum);
// ====== creating an array to push all networkThings Keys & values ======
let nameOfNetworks = [];
for (let networks in networkThings) {
nameOfNetworks.push(networks);
// const nwt = nameOfNetworks.push(networks);
// console.log(nwt);
}
// ====== looping through the created array above ======
for (const item of nameOfNetworks) {
// ====== using the key to access values inside of the object(networkThings) ======
let array = networkThings[item];
// iterating through array to find a match of the first four numbers
let first4 = phoneNum.slice(0, 4);
for (let i = 0; i < array.length; i++) {
// if first4 matches values inside array
if (first4 === array[i] && phoneNum.length <= 11) {
//console.log(item);
return item;
}
}
}
return "";
}
function showLogo(network) {
switch (network) {
case "mtn":
// console.log("we are here");
mtnLogo.classList.remove("noloveLogo");
break;
case "airtel":
// console.log("we are here");
airtelLogo.classList.remove("noloveLogo");
break;
case "glo":
// console.log("we are here");
gloLogo.classList.remove("noloveLogo");
break;
case "etisalat":
// console.log("we are here");
etisalatLogo.classList.remove("noloveLogo");
break;
default:
const array = [mtnLogo, airtelLogo, gloLogo, etisalatLogo];
array.forEach((x) => {
x.classList.add("noloveLogo");
});
break;
}
}
function removeLogo() {
const array = [mtnLogo, airtelLogo, gloLogo, etisalatLogo];
array.forEach((x) => {
// // console.log(x);
x.classList.add("noloveLogo");
});
}
}
// ======= DO NOT EDIT ============== //
export default startApp;
// ======= EEND DO NOT EDIT ========= //
/*
- We want to get the number from the input field
- We want to compare that number against the number in our array
- If we find a match, we want to show the corresponding logo
*/