forked from iub-cse-shq/Basic-JS-Exercises
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasicJSExercises.js
More file actions
284 lines (213 loc) · 6.8 KB
/
Copy pathbasicJSExercises.js
File metadata and controls
284 lines (213 loc) · 6.8 KB
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/* ---------------------------
*** #1 Percentage ***
Write a JavaScript function to calculate the percentage (%) of a given number.
Test:
console.log(percentage(2000, 37.12));
Output:
742.4
--------------------------- */
function percentage(number, percentage) {
var calculate = (number/100)*percentage;
return calculate;
}
console.log("Percentage Calculator:");
/* Uncomment the following to check */
console.log(percentage(2000, 37.12));
console.log(percentage(450, 56.5));
console.log(percentage(5230, 34));
/* ---------------------------
*** #2 Area of Triangle ***
Write a JavaScript function to calculate the area of a triangle given the base and height.
Test:
console.log(areaTriangle(5, 20));
Output:
50
--------------------------- */
function areaTriangle(base, height) {
return base*height/2;
}
console.log("Triangle Area Calculator:");
/* Uncomment the following to check */
console.log(areaTriangle(2, 7));
console.log(areaTriangle(20, 56.5));
console.log(areaTriangle(50, 34));
/* ---------------------------
*** #3 Rotate String ***
Rotate a given string in the right direction by periodically removing
one letter from the end of the string and attaching it to the front.
Test:
rotateString("cat");
Output:
cat
tca
atc
cat
HINT: Use substring()
--------------------------- */
function rotate_string(text) {
console.log(text);
for(var i=0;i<text.length;i++){
text = text[text.length-1] + text.substring(0, text.length-1);
console.log(text);
}
}
console.log("Rotate String:");
/* Uncomment the following to check */
rotate_string("cat");
rotate_string("pseudonym")
/* ---------------------------
*** #4 Hide part of email address ***
Write a JavaScript function to hide email addresses to protect from unauthorized user.
Test:
console.log(protect_email("tom_jenkins@example.com"));
Output:
"tom_j...@example.com"
HINT: Use split() and substring()
--------------------------- */
function protect_email(email) {
var array = email.split('@');
return array[0].substring(0,5) + "..." +'@'+array[1];
}
console.log("Protected email:");
/* Uncomment the following to check */
console.log(protect_email("harry_potter@gmail.com"));
console.log(protect_email("sarah.connor@gmail.com"));
console.log(protect_email("tom_jenkins@example.com"));
/* ---------------------------
*** #5 Remove First Occurence ***
Write a JavaScript function to remove the first occurrence of a given 'search string' from a string.
Test:
console.log(remove_first_occurrence("The quick brown fox jumps over the lazy dog", 'the'));
Output:
"The quick brown fox jumps over lazy dog"
HINT: Use indexOf() and slice()
--------------------------- */
function remove_first_occurrence(text, searchstring) {
text = text.slice(0,text.indexOf(searchstring)) + text.slice(text.indexOf(searchstring)+searchstring.length+1)
return text;
}
console.log("Remove First Occurrence:");
/* Uncomment the following to check */
console.log(remove_first_occurrence("The quick brown fox jumps over the lazy dog", 'the'));
console.log(remove_first_occurrence("Drastic times call for drastic measures", 'drastic'));
/* ---------------------------
*** #6 Alphabetical Order ***
Write a JavaScript function that returns a passed string with letters in alphabetical order.
Test:
console.log(alphabetic_order('textbook'));
Output:
bekoottx
HINT: Use join(), split() and sort() functions
--------------------------- */
function alphabetic_order(word) {
return word.split("").sort().join("");
}
console.log("Alphabetic Order:");
/* Uncomment the following to check */
console.log(alphabetic_order("textbook"));
console.log(alphabetic_order("webmaster"));
console.log(alphabetic_order("supercalifragilisticexpialidocious"));
/* ---------------------------
*** #7 Most Frequent Item ***
Write a JavaScript function to find the most frequent item in a given array.
Test:
most_frequent([3, 'c', 'c', 'c', 2, 3, 'c', 3, 'c', 2, 4, 9, 3]);
Output:
c occurs 5 times
--------------------------- */
function most_frequent(arr) {
var item;
var max=0 ;
var count = 0;
for(var i=0;i<arr.length;i++){
for(var j=i;j<arr.length;j++){
if(arr[i]===arr[j]){
count++;
}
}
if(count>max){
item = arr[i];
max = count;
}
count = 0;
}
console.log(item + " occurs " + max + " times");
}
console.log("Most Frequent Item:");
/* Uncomment the following to check */
most_frequent([3, 'c', 'c', 'c', 2, 3, 'c', 3, 'c', 2, 4, 9, 3]);
most_frequent([7, 2, 'ax', '9', 9, 'ax', 'ax']);
/* ---------------------------
*** #8 Remove Duplicate Values ***
Write a JavaScript program to find and remove duplicate values in a JavaScript array.
Test:
remove_duplicates([3, 'a', 'a', 'a', 2, 3, 'a', 3, 'a', 2, 4, 9, 3]);
Output:
[3, 'a', 2, 4, 9]
--------------------------- */
function remove_duplicates(arr) {
var duplicateRemoved = [];
var isexists = false;
for(var i=0;i<arr.length;i++){
for(var j=0;j<duplicateRemoved.length;j++){
if(arr[i]===duplicateRemoved[j]){
isexists = true;
}
}
if(!isexists){
duplicateRemoved[duplicateRemoved.length]= arr[i];
}
isexists = false;
}
console.log(duplicateRemoved);
}
console.log("Remove Duplicate Values:");
/* Uncomment the following to check */
remove_duplicates([3, 'a', 'a', 'a', 2, 3, 'a', 3, 'a', 2, 4, 9, 3]);
remove_duplicates([4, 4, 4, 5, 's', 8, 's']);
/* ---------------------------
*** #9 Dash between Even Numbers ***
Write a JavaScript program which accepts a number as input and inserts dashes (-) between two consecutive even numbers.
Test:
dash_in_even(012345684);
Output:
"012-456-8-4"
--------------------------- */
function dash_in_even(number) {
var str = "";
number = number.toString();
for(var i=0;i<number.length;i++){
str = str + number[i];
if(number[i]%2 == 0 && number[i+1]%2 == 0){
str = str + '-';
}
}
console.log(str);
}
console.log("Dash between Even Numbers:");
/* Uncomment the following to check */
dash_in_even(100);
dash_in_even(1356);
dash_in_even(246824);
dash_in_even(1324567824);
/* ---------------------------
*** #10 Guessing Game ***
Write a JavaScript program where the program takes a random integer between 1 to 10,
the user is then prompted to input a guess number. If the user input matches with guess number,
the program will display a message "Good Work" otherwise display a message "Not matched"
HINT: Use Math.ceil() and Math.random()
--------------------------- */
function guessing_game(guess) {
var rand = Math.ceil(Math.random()*10);
if(guess == rand){
console.log("Good Work");
}
else{
console.log("Not Matched");
}
}
console.log("Guessing Game:");
/* Uncomment the following to check */
var guess = prompt('Guess the number between 1 and 10 inclusive');
console.log("User guessed: "+ guess);
guessing_game(guess);