-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
227 lines (211 loc) · 5.46 KB
/
main.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
"use strict";
const capital = document.querySelector(".capital");
const padstart = document.querySelector(".padstart");
const padend = document.querySelector(".addup");
const camelcase = document.querySelector(".camelcase");
const reverse = document.querySelector(".reverse");
const trim = document.querySelector(".trim");
const repeats = document.querySelector(".repeat");
const input = document.querySelector(".input-filed");
const check = document.querySelector(".check");
const code = document.querySelector(".code");
const answer = document.querySelector(".answer");
//state...
//how we track which function is running
let currentfunc = 0;
//functions to run
//function to pad at the start with 7 xxx
const padAtStart = function (n) {
const len = n.length + 7;
return n.padStart(len, "x");
};
//function to pad at the end with 10 xxx
const padAtEnd = function (n) {
const len = n.length + 10;
return n.padEnd(len, "x");
};
//function to make first letter capital one...
const capitaFirst = function (s) {
const sArray = s.split(" ");
const fined = [];
for (const f of sArray) {
const [first, ...other] = f.split("");
fined.push([first.toUpperCase(), ...other].join(""));
}
return fined.join(" ");
};
//function to make string into camel case
const camelCase = function (s) {
const sArr = s.split(" ");
let [first, ...second] = sArr;
let cameled = [first];
for (const ss of second) {
cameled.push(ss.replace(ss[0], ss[0].toUpperCase()));
}
return cameled.join("");
};
//function to reverse a word
const reverseWords = function (s) {
const sArr = s.split(" ");
return sArr.reverse().join(" ");
};
//function to trim words
const trimWord = function (s) {
return s.split(" ").join("");
};
//function to repeat a word
const repeatWord = function (s) {
s += " ";
return s.repeat(`${s.length}`);
};
capital.addEventListener("click", function () {
currentfunc = 1;
const str = `
//this will capitalize first letter <br>
<br>
function capitaFirst(string) {<br>
<br>
const sArray = string.split(' ');<br>
const fined = [];<br>
for (const f of sArray) {<br>
const [first, ...other] = f.split('');<br>
fined.push([first.toUpperCase(), ...other].join(''));<br>
}<br>
return fined.join(' ');<br>
<br>
};
`;
code.innerHTML = "";
code.insertAdjacentHTML("afterbegin", str);
input.value = "";
answer.textContent = "";
});
padstart.addEventListener("click", function () {
currentfunc = 2;
const str = `
//this will padstart the string <br>
<br>
function padAtStart(string) {<br>
<br>
const len = string.length + 7;<br>
return string.padStart(len, 'x');<br>
<br>
};
`;
code.innerHTML = "";
code.insertAdjacentHTML("afterbegin", str);
input.value = "";
answer.textContent = "";
});
padend.addEventListener("click", function () {
currentfunc = 3;
console.log(currentfunc);
// console.log('clicked');
const str = `
//this will padend the string <br>
<br>
function padAtEnd(string) {<br>
<br>
const len = string.length + 10;<br>
return string.padEnd(len, 'x');<br>
<br>
};
`;
code.innerHTML = "";
code.insertAdjacentHTML("afterbegin", str);
input.value = "";
answer.textContent = "";
});
camelcase.addEventListener("click", function () {
currentfunc = 4;
const str = `
//this will camelcase the words <br>
<br>
function camelCase(string) {<br>
<br>
const sArr = string.split(' ');<br>
let [first, ...second] = sArr;<br>
let cameled = [first];<br>
for (const ss of second) {<br>
cameled.push(ss.replace(ss[0], ss[0].toUpperCase()));<br>
}<br>
return cameled.join('');<br>
<br>
};
`;
code.innerHTML = "";
code.insertAdjacentHTML("afterbegin", str);
input.value = "";
answer.textContent = "";
});
reverse.addEventListener("click", function () {
currentfunc = 5;
const str = `
//this will reverse the string <br>
<br>
function reverseWords(string) {<br>
<br>
const sArr = string.split(' '); <br>
return sArr.reverse().join(' '); <br>
<br>
};
`;
code.innerHTML = "";
code.insertAdjacentHTML("afterbegin", str);
input.value = "";
answer.textContent = "";
});
trim.addEventListener("click", function () {
currentfunc = 6;
const str = `
//this will trim the string <br>
<br>
function trimWord(string) {<br>
<br>
return s.split(' ').join(''); <br>
<br>
};
`;
code.innerHTML = "";
code.insertAdjacentHTML("afterbegin", str);
input.value = "";
answer.textContent = "";
});
repeats.addEventListener("click", function () {
currentfunc = 7;
const str = `
//this will repeat the string <br>
<br>
function repeatWord(string) {<br>
<br>
return string.repeat(string.length); <br>
<br>
};
`;
code.innerHTML = "";
code.insertAdjacentHTML("afterbegin", str);
input.value = "";
answer.textContent = "";
});
check.addEventListener("click", function () {
let text = input.value;
let func =
currentfunc === 1
? capitaFirst(text)
: currentfunc === 2
? padAtStart(text)
: currentfunc === 3
? padAtEnd(text)
: currentfunc === 4
? camelCase(text)
: currentfunc === 5
? reverseWords(text)
: currentfunc === 6
? trimWord(text)
: currentfunc === 7
? repeatWord(text)
: (answer.textContent = "select a function first..");
const answed = func;
answer.textContent = answed;
input.value = "";
});