forked from zero-to-mastery/JS_Fun_Practice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmy_solution_itsnou.js
128 lines (93 loc) · 2.11 KB
/
my_solution_itsnou.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
//1)
const identify=x=>x
//2)
const addb= (a,b) => a+b
//3)
const subb= (a,b) => a-b
//4)
const mulb= (a,b) => a*b
//5)
const minb= (a,b) =>{
if(a>b){
return b
}else return a
}
// other solution
const minb= (a,b) => a<b ? a : b
//6)
const maxb= (a,b) => a>b ? a : b
//other solution
function maxb(a,b){
if(a>b){
return a
}else return b
}
//7)
const add= (...nums)=> nums.reduce((num, nums)=> num+nums)
//8)
const sub= (...varios)=> varios.reduce((num,varios)=>num-varios)
//9)
const mul = (...nums)=> nums.reduce((num,nums)=> num*nums)
//10)
function min(...nums){
let menor=nums[0]
for(let i=1;i<nums.length;i++){
if(nums[i]<menor){
menor=nums[i]
}
}
return menor
}
//11)
function max(...nums){
let mayor= nums[0]
for(let i=1;i<nums.length;i++){
if(nums[i]>mayor){
mayor=nums[i]
}
}return mayor
}
//12) Write a function addRecurse that is the generalized add function but uses recursion
function addReduce(...nums){
let sum=0
for(let i=0;i<nums.length;i++){
sum= sum+nums[i]
}
return sum
}
//13) Write a function mulRecurse that is the generalized mul function but uses recursion
function mulRecurse(...nums){
let mul= nums[0]
for(let i=1;i<nums.length;i++){
mul=mul*nums[i]
}
return mul
}
//14) Write a function minRecurse that is the generalized min function but uses recursion
function minRecurse(...nums) {
let menor=nums[0]
for(let i=1;i<nums.length;i++){
if(nums[i]<menor){
menor=nums[i]
}
}return menor
}
//15) Write a function maxRecurse that is the generalized max function but uses recursion
function maxRecurse(...nums){
let mayor=nums[0]
let i=0
while(i<nums.length){
if(nums[i]>mayor){
mayor=nums[i]
}i++
}return mayor
}
//16) Write a function not that takes a function and returns the negation of its result
const isOdd= (num)=>{
return (num%2!=0?false:true)
}
const not= (func)=>{
return !func
}
console.log(not(isOdd(2)))
console.log('--------------')