-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpracticeProblem2.js
More file actions
341 lines (238 loc) · 7.7 KB
/
practiceProblem2.js
File metadata and controls
341 lines (238 loc) · 7.7 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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
//page two of http://www.w3resource.com/javascript-exercises/javascript-basic-exercises.php
console.log("Problem1"); // need to look this one up
console.log("---------------------");
console.log("Problem2");
var stringed = "string";
var isPalindrome = function(string){
for(var i = 0; i < string.length/2; i++){
//string.remove(string.indexOf(" ")); add for palindromic phrases need internet
console.log(string[i]);
console.log(string[(string.length - 1) - i]);
if(string[i] != string[(string.length - 1) - i]){
return false;
break;
}
}
return true;
};
console.log(isPalindrome("AbbbA"));
console.log("---------------------");
console.log("Problem3");
var factorial = function(number){
var result = 1;
for(var i = number; i > 0; i--){
result = i * result;
}
return result;
};
//console.log("dog".substr(0,1));
//console.log("dog".substr(0,2));
//console.log("dog".substr(0,3));
//console.log("dog".substr(0,1));
//console.log("dog".substr(0,1));
//console.log("dog".substr(0,1));
var allCombo = function(string){
var counter = factorial(string.length);
var results = "";
for(var i = 0; i < string.length ; i++){
for(var y = 1; y <= string.length ; y++){
console.log(i + ", " + y);
console.log(string[i,y]);
}
}
return results;
};
//console.log(factorial("chuck".length));
//console.log(allCombo("doge"));//will be back for this research range of string
console.log("---------------------");
console.log("Problem 4");
var greatestChar = function(string){
var result = "";
for(var i = 0; i < string.length; i++){
for(var y = 0; y <string.length; y++){
if(string[i] < string[y]){
console.log(string[i] + " "+ string[y]);
break;
}
else{
result = result + string[i];
console.log(result);
}
}
}
}
//greatestChar("webmaster"); I am tired
console.log("------------------ \n Problem 5");
var properCase = function(string){
var result = "";
result = result + string[0].toUpperCase();
for(var i = 1; i < string.length; i++){
if(string[i - 1] == " "){
result = result + string[i].toUpperCase();
}
else{
result = result + string[i];
}
}
return result;
};
console.log(properCase("frog goes to the store"));
console.log("----------------------- \n Problem 6");
var longestWord = function(string){
var longest = [];
var word = ""
//longest.push("hey");
for(var i = 0; i < string.length; i++){ // this loop converts strings to arrays!!
if(string[i - 1] == " "){
//word = word + string[i];
longest.push(word);
word = "";
word = word + string[i];
}
else if(i == string.length - 1){
word = word + string[i];
longest.push(word);
}
else if(string[i] != " "){
word = word + string[i];
}
}
console.log(longest);
var counter = 0;
while(longest.length > 1){
if(longest[counter].length <= longest[counter + 1].length){
//console.log(longest[counter].length);
//console.log(longest[counter +1 ].length);
longest.pop(longest[counter + 1]);
counter = -1;
}
counter++;
}
console.log(longest);
};
//longestWord("hey there you"); // ugh come back to this with internet
console.log("---------------------");
console.log("Problem 7")
var numVowels = function(string){
var numberV = 0;
for(var i = 0; i < string.length; i++){
//console.log(string[i]);
if(string[i] == "a" | string[i] == "e" | string[i] == "o" | string[i] == "i" | string[i] == "u"){
numberV++
}
}
return numberV;
};
console.log(numVowels("abeea"));
console.log("---------------------");
console.log("Problem 8");
var isPrime = function(number){
var primes = [2];
for(var i = 0; i < (number/2) + 2; i++){
if(number % primes[i] == 0 && primes[i] != number){
//console.log(primes[i]);
return "not prime";
break;
}
else{
primes.push(primes[i]+1);
//console.log(primes);
}
}
return "yep it is prime"
};
console.log(isPrime(59)); // test with more numbers but i think that it is right
console.log("---------------------");
console.log("Problem 9");
var typer = function(thing){
return typeof(thing);
};
console.log(typer(isPrime));
console.log("---------------------");
console.log("Problem 10");// not sure what an identity matrix is but here goes
var gridMaker = function(number){
var array = [];
for(var i = 0; i < number; i++){
temparray = [];
for(var y = 0; y < number; y++){
temparray.push(number);
}
array.push(temparray);
}
//array.pop(array[0]);
return array;
};
console.log(gridMaker(5)); //maybe works, depends on what an identity matrix is
console.log("---------------------");
console.log("Problem 11");
var seconds = function(array){
res = [];
for(var i = 0; i < array.length; i++){
for(var y = 0; y < array.length; y++){
if(array[i] < array[y]){
console.log(array[i] + "<" + array[y]);
break;
}
else{
console.log(array[i] + ">" + array[y]);
//res.push(array[y]);
}
}
}
res.push(array[i]);
return res;
};
//console.log(seconds([1,7,2,4,5])); need to research comparison down arrays
console.log("---------------------");
console.log("Problem 12");
var isPerfect = function(number){
var divisors = [];
for(var i = number - 1; i>=1; i--){
if(number % i == 0){
divisors.push(i);
}
}
if(divisors.reduce(function(a,b){return a+b;}, 0) == number){
return "its perfect";
}
else{
return "not perfect";
}
};
console.log(isPerfect(8128));
console.log("---------------------");
console.log("Problem 13");
var getFactors = function(number){
var factors = [];
for(var i = 0; i <= number; i++){
for(var y = 0; y <= number; y++){
if(i*y== number){
factors.push(i);
}
}
}
return factors;
};
console.log(getFactors(67));// this works how can it be optimized the 10000 is the largest that works
console.log("---------------------");
console.log("Problem 15");
var toCoins = function(amount, array){
}
var power = function(b,n){
var res = 1;
for(var i = 0; i < n; i++){
res = res * b;
}
return res;
}
console.log(power(2,8));// it works
console.log("---------------------");
console.log("Problem 16");
var uniqueLetters = function(string){
var res = "";
for(var i = 0; i < string.length; i++){
for(var y = 0; y < string.length; y++){
}
}
return res;
}