-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
171 lines (150 loc) Β· 4.11 KB
/
app.js
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
const output = document.querySelector("#output");
const output2 = document.querySelector("#output-2");
const container = document.querySelector(".container");
const body = document.querySelector("body");
const pushBtn = document.querySelector("#push-item");
const popBtn = document.querySelector("#pop-item");
const shiftBtn = document.querySelector("#shift-item");
const unshiftBtn = document.querySelector("#unshift-item");
const clearAllBtn = document.querySelector("#delete-all-items");
const shuffleBtn = document.querySelector("#shuffle-array");
const insertBtn = document.querySelector("#insert-item");
const deleteItemBtn = document.querySelector("#delete-one-item");
const darkModeBtn = document.querySelector("#btn-dark-mode");
const insertIndex = document.querySelector("#num");
const deleteIndex = parseInt(document.querySelector("#num-delete").value);
const items = [];
const emojisArray = [
"π",
"β",
"π",
"π",
"π",
"π",
"π€",
"β",
"πͺ",
"π",
"π",
"π",
"π",
"π",
"β",
"π",
"β₯",
"π",
"π",
"π",
"π°",
"β",
"β",
"π",
"β",
"π",
"β",
"π«",
"β",
"πΈ",
"π",
"π",
"π",
"π",
"π",
];
// checks if the array is empty
if (items.length === 0) {
output.innerHTML = "[ Array is empty. Add new items! ]";
}
// checks the length of the array
const checkLengthOfArray = (arr) => {
arr.length ? alert("There are 10 items in the array") >= 10 : "";
};
// pushs items to an array
const addItem = (e) => {
e.preventDefault();
let i = randomIndex();
items.push(emojisArray[i]);
output.innerHTML = `[ ${items.join("")}]`;
};
// generates random index
const randomIndex = () => Math.floor(Math.random() * emojisArray.length);
// adds items to the the beginning of the array
const addItemToBegin = (e, i) => {
e.preventDefault();
i = randomIndex();
items.unshift(emojisArray[i]);
output.innerHTML = `[ ${items.join("")}]`;
};
// insert items to specific index // should be revised
// const insertItem = (arr,index,el) => arr.splice(index-1, 0, el);
// ar = [1,2,3,4,5]
// let x = ar.splice(0,3,)
// console.log(x)
// removes last item from the array
const popItem = (e) => {
e.preventDefault();
items.pop();
output.innerHTML = `[ ${items.join("")}]`;
};
// removes first item from the array
const removeFirstItem = (e) => {
e.preventDefault();
items.shift();
output.innerHTML = `[ ${items.join("")}]`;
};
// delete all items
const clearArray = (e) => {
e.preventDefault();
items.length = 0;
output.innerHTML = `[ ${items}]`;
};
//shuffle items in the array
const shuffleArray = (e) => {
e.preventDefault();
items.sort(() => Math.random() - 0.5);
output.innerHTML = `[ ${items.join("")}]`;
};
//const filterItem = (arr, val) => arr.filter(i => i != val);
// deletes one item from a given index
const deleteOneItem = (e) => {
e.preventDefault();
if (deleteIndex > -1) {
items.splice(deleteIndex - 1, 1);
}
output.innerHTML = `[ ${items.join("")}]`;
};
//addItemToBegin(randomIndex());
//addItem(randomIndex());
//items.forEach(i => console.log(i))
//popItem();
//removeFirstItem();
//console.log(items)
//console.log(insertItem(0,0,2))
// for (let i = 0; i <= 6; i++) {
// addItem(i);
// }
// console.log(items);
// document.write("\n");
// document.write(deleteOneItem(items, 1));
//console.log(removeFirstItem())
//console.log(items)
//clearArray(items)
//console.log(shuffleArray(items))
// event listeners
pushBtn.addEventListener("click", addItem);
popBtn.addEventListener("click", popItem);
shiftBtn.addEventListener("click", removeFirstItem);
unshiftBtn.addEventListener("click", addItemToBegin);
clearAllBtn.addEventListener("click", clearArray);
shuffleBtn.addEventListener("click", shuffleArray);
deleteItemBtn.addEventListener("click", deleteOneItem);
darkModeBtn.addEventListener("click", (e) => {
e.preventDefault();
container.classList.toggle("dark-mode");
darkModeBtn.innerHTML = container.classList.contains("dark-mode")
? "light mode"
: "dark mode";
body.style.backgroundColor = container.classList.contains("dark-mode")
? "#A700AF"
: "";
});