-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeneral.js
92 lines (70 loc) · 1.75 KB
/
general.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
function replaceAt(old_string, char, index) {
old_string = old_string.slice(0, index) + char + old_string.slice(index+1);
return old_string;
}
function allInstancesOf(c, string) {
let indices = [];
for (let i = 0; i < string.length; i++) {
if (string.charAt(i) == c) {
indices.push(i);
}
}
return indices;
}
function clearHTML(element) {
element.innerHTML = "";
}
function setHTML(element, html) {
element.innerHTML = html;
}
function clearValue(element) {
element.value = "";
}
function createElement(object, html, class_name, id) {
let new_object = document.createElement(object);
new_object.setAttribute('class', class_name);
new_object.setAttribute('id', id);
new_object.innerHTML = html;
return new_object;
}
function count(string, char) {
let count = 0;
for (let i = 0; i < string.length; i++) {
if (string[i] == char) count++;
}
return count;
}
function combineLists(a, b) {
return [...new Set(a.concat(b))];
}
function pluralOrSingle(quantity, singular, plural) {
if (quantity == 1) {
return singular;
}
return plural;
}
function intToChar(int) {
return String.fromCharCode(int);
}
function charToInt(char) {
return char.charCodeAt(0);
}
function isEmpty(list) {
return list.length == 0;
}
function decimalToPercent(num) {
return (num*100).toFixed(2) + "%";
}
function randomElementOf(list) {
let index = Math.floor(Math.random()*list.length);
return list[index];
}
function swapDivs(event, elem) {
elem.parentNode.insertBefore(elem, event);
}
function extendArray(array, new_max, value) {
for (let i = 0; i < new_max; i++) {
if (!array[i]) array[i] = value;
}
return array;
}