-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
642 lines (507 loc) · 10.4 KB
/
main.js
File metadata and controls
642 lines (507 loc) · 10.4 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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
/*
1
Write a function named tellFortune that:
takes 4 arguments: number of children,
partner's name, geographic location, job title.
outputs your fortune to the screen like so:
"You will be a X in Y, and married to Z with N kids."
Ex: tellFortune('software engineer', 'Jordan', 'Alice', 3);
=> "You will be a software engineer in Jordan, and married to Alice with 3 kids."
*/
var x= "software engineer ";
var y= "jordan";
var z="alice";
var n =3 ;
console.log(tellFortune('software engineer', 'Jordan', 'Alice', 3));
function tellFortune(x,y,z,n){
return ('You will be a ' + x + " "+'in' +" " + y + ', and married to' + " "+ z + " "+'with' + " "+ n + " "+'kids.');
}
/*
2
Write a function named calculateDogAge that:
takes 1 argument: your puppy's age.
calculates your dog's age based on the conversion
rate of 1 human year to 7 dog years.
outputs the result to the screen like so:
"Your doggie is NN years old in dog years!"
Ex: calculateDogAge(1);
=> "Your doggie is 7 years old in dog years!"
*/
var n=1 * 7;
console.log(calculateDogAge(7));
function calculateDogAge(n){
return ("Your doggie is" +" " +n+ " "+ "years old in dog years!");
}
/*
3
Write a function named calculateSupply that:
takes 2 arguments: age, amount per day.
calculates the amount consumed for rest of the life (based on a constant max age 100).
outputs the result to the screen like so:
"You will need NN to last you until the ripe old age of X"
Ex: calculateSupply(30, 3);
=> 'You will need 76650 cups of tea to last you until the ripe old age of 100;
*/
var n= 30*3;
var x=100;
console.log(calculateSupply(90,100));
function calculateSupply(n,x){
return("You will need " + n + "cups of tea to last you until the ripe old age of" + x );
}
/*
4
Write a function called greet that:
takes 1 argument: name.
and it will return hello + name
Ex: greet("Adam")
=> "Hello Adam"
*/
var name;
console.log(greet("adam"));
function greet(name){
return("hello"+" " + name);
}
/*
5
what is the error:
function double(cat) {
return 2 * x;
}
we didnt declare the var x
function double(7) {
return 2 * 7;
}
the error:double(7)
function double('7') {
return 2 * 'x';
}
the error:'x' and '7'
*/
/*
6
fix these functions:
func double1(x) {
return 2 * x ;
}
functiondouble2(x)
return 2 * x;
function (x) double3 {
return 2 * x;
*/
var x;
console.log(double1(1.1));
function double1(x) {
return 2 * x ;
}
var x;
console.log(double2(2.1))
function double2(x){
return 2 * x;}
var x;
console.log(double3(3.2));
function double3 (x){
return 2 * x;
}
/*
7
Write a function called cube that:
accept 1 parameter and calculate the cube of this number
Ex: cube(4)
=> 64
*/
var x;
console.log(cube(4));
function cube(x){
return (x*x*x);
}
/*
8
Write a function called multiply that:
accept 2 parameters and calculate the multiply of these 2 numbers
Ex: multiply(3,4)
=> 12
Ex: multiply(5,4)
=> 20
*/
var x;
var y;
console.log(multiply(4,3));
console.log(multiply(5,4));
function multiply(x,y){
return x*y;
}
/*
9
Write a function called canIGetADrivingLicense that:
accept 1 parameter represent the age
and if the age greater than or equal to 20 return "yes you can"
otherwise return "please come back after X years to get one"
Ex: canIGetADrivingLicense(21)
=> "yes you can"
Ex: canIGetADrivingLicense(17)
=> "please come back after 3 years to get one"
Ex: canIGetADrivingLicense(20)
=> "yes you can"
*/
var x;
console.log(canIGetADrivingLicense(22));
function canIGetADrivingLicense(x){
if(x>=20){
return ("yes you can");
}
else{
return ("please come back after 3 years to get one");
}
}
/*
10
Write a function called sameLength
that accepts two strings as arguments,
and returns true if those strings have the same length, and false otherwise.
**hint: how we can know string length Ex: : "tree".length => 4
Ex: sameLength("tree","clue")
=> true
Ex: sameLength("tree","car")
=> false
*/
var x;
var y;
console.log(sameLength("tree","clue"));
function sameLength(x,y){
if(x.length == y.length) {
return("true");
}
else{
return ("false");
}
}
/*
11
Write a function called largerNubmer
that accept two numbers as arguments,
and return the first larger numbers
Ex: largerNubmer(5,6)
=> 6
Ex: largerNubmer(5,3)
=> 5
*/
var x;
var y;
console.log(largerNubmer(5,6));
console.log(largerNubmer(7,9));
function largerNubmer(x,y){
if(x>y){
return(x);
}
else{
return(y);
}
}
/*
12
Write a function called smallerNubmer
that accept three numbers as arguments,
and return the first smaller number
Ex: smallerNubmer(8,6,7)
=> 6
Ex: smallerNubmer(5,99,34)
=> 5
Ex: smallerNubmer(5,99,3)
=> 3
Ex: smallerNubmer(5,3,3)
=> 3
*/
var x;
var y;
var z;
console.log(smallerNubmer(8,6,7));
console.log(smallerNubmer(5,99,34));
console.log(smallerNubmer(5,3,3));
function smallerNubmer(x,y,z){
if(x<y && x<z){
return x;
}
else if (y<x && y<z) {
return y;
}
else{
return z;
}
}
/*
13
Write a function called shorterString
that accept five string as an arguments,
and return the first shorter string
Ex: shorterString("air","school","car","by","github")
=> by
Ex: shorterString("air","tr","car","by","github")
=> tr
Ex: shorterString("by","tr","car","air","github")
=> by
Ex: shorterString("air","by","car","school","github")
=> by
Ex: shorterString("air","tr","by","car","github")
=> by
Ex: shorterString("air","tr","car","github","by")
=> by
*/
function shorterString (x,y,z,a,b){
let arr = [x,y,z,a,b];
let short = Math.min(x.length,y.length,z.length,a.length,b.length);
for (let num=0;num<5;num++){
if (arr[num].length== short)
{
return arr[num];
}
}
}
console.log(shorterString("air","tr","car","github","by"));
console.log(shorterString("air","tr","car","by","github"));
console.log(shorterString("by","tr","car","air","github"));
console.log(shorterString("air","by","car","school","github"));
console.log(shorterString("air","tr","car","github","by"));
console.log(shorterString("air","tr","car","github","by"));
/*
14
Write a function called longerString
that accept four string as an arguments,
and return the first longer string
Ex: longerString("air","school","car","github")
=> school
Ex: longerString("air","schoo","car","github")
=> github
try all the cases (change the order of the longestString)
*/
function longerString (x,y,z,a,b){
let arr1 = [x,y,z,a,b];
let long = Math.max(x.length,y.length,z.length,a.length,b.length);
for (let num1=0;num1<5;num1++){
if (arr1[num1].length== long)
{
return arr1[num1];
}
}
}
console.log(longerString("air","tr","car","github","by"));
console.log(longerString("air","tr","car","by","github"));
console.log(longerString("by","tr","car","air","github"));
console.log(longerString("air","by","car","school","github"));
console.log(longerString("air","tr","car","github","by"));
console.log(longerString("air","tr","car","github","by"));
/*
15
Write a function called isEven
that accept 1 argument as a number,
and return true if this number is even
and false if this number is odd
Ex: isEven(1)
=> false
Ex: isEven(2)
=> true
*/
var x;
console.log(isEven(2));
function isEven(x){
if (x % 2 ==0){
return("true");
}else{
return("false");
}
}
/*
16
Write a function called isOdd
that accept 1 argument as a number,
and return true if this number is Odd
and false if this number is Even
Ex: isOdd(4)
=> false
Ex: isOdd(5)
=> true
*/
var x;
console.log(isOdd(3));
function isOdd(x){
if (x % 2 ==1){
return("true");
}else{
return("false");
}
}
/*
17
Write a function called positive
that accept 1 argument as a number,
and return the positive version of the number passed
Ex: positive(4)
=> 4
Ex: positive(-5)
=> 5
*/
var x;
console.log(positive(-5));
function positive(x){
return (Math.abs(x));
}
/*
18
Write a function called fullName
that accept two parameters, firstName and lastName,
and returns the firstName and lastName concatenated
together with a space in between.
Ex: fullName("Adam","McCallen")
=> "Adam McCallen"
Ex: fullName("Alex", "Mercer")
=> "Alex Mercer"
*/
var x;
var y;
console.log(fullName("Farah","Mango"));
function fullName(x,y){
return x+" "+y
;
}
/*
19
Write a function called average
that takes five numbers as inputs
and returns the average of those numbers.
Ex: average(1,2,3,4,5)
=> 3
Ex: average(5,7,9,3,5)
=> 5.8
*/
var x;
var y;
var z;
var a;
var b;
console.log(average(1,2,3,4,5));
function average(x,y,z,a,b){
var c= (x+y+z+a+b)/5;
return (c);
}
/*
20
Write a function called randomNumber
that didnt takes any parameter
and returns a random number between 0-1
** hint: you can seacrh using MDN
Ex: randomNumber()
=> 0.2278
Ex: randomNumber()
=> 0.475
*/
console.log(randomNumber());
function randomNumber(){
return (Math.random());
}
/*
21
Write a function called randomBetweenNumbers
that takes 2 parameters
and returns a random number between them
** hint: you can seacrh using MDN
Ex: randomBetweenNumbers(1,8)
=> 7.5412
Ex: randomBetweenNumbers(3,100)
=> 23
*/
var x;
var y;
console.log(randomBetweenNumbers(100,3));
function randomBetweenNumbers(x,y){
return Math.floor((Math.random() * (x-y+1)+y));
}
/*
22
Write a function called scoreInUniversty
that takes 1 parameters
and returns the alpabet in the unevirsty
A => 95-100
B => 85-94
C => 70-84
D=> 50-69
F=> 0-49
Ex: scoreInUniversty(96)
=> "A"
Ex: scoreInUniversty(3)
=> "F"
Ex: scoreInUniversty(71)
=> "C"
*/
var x;
console.log(scoreInUniversty(95));
function scoreInUniversty(x){
if (x >=95 && x<=100){
return ("A")
}
else if (x>=85 && x<=94){
return("B")
}
else if (x>=70 && x<=84){
return("C")
}
else if (x>=50 && x<=69){
return("D")
}
else{
return ("F")
}
}
/*
23
Write a function called counter
that will returns a number bigger
than the one that returnd before
and start from 0
Ex: counter()
=> 1
Ex: counter()
=> 2
Ex: counter()
=> 3
*/
let v=-1;
function counter (){
v++
console.log("" +v);
}
counter();
counter();
counter();
/*
24
Write a function called resetCounter
that will reset the previuos function
and return the number before reset and
a string say that the counter reset
Ex: counter()
=> 1
Ex: counter()
=> 2
Ex: counter()
=> 3
Ex: resetCounter()
=> 3 and the counter reset now
Ex: counter()
=> 1
Ex: counter()
=> 2
Ex: resetCounter()
=> 2 and the counter reset now
Ex: counter()
=> 1
*/
function resetCounter(){
console.log(""+ v + "and the counter reset now")
v=-1
}
counter();
counter();
counter();
resetCounter();
counter();
counter();
counter();