Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
265 lines (195 loc) · 6.13 KB
/
Copy pathscript.js
File metadata and controls
265 lines (195 loc) · 6.13 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
// Look at the example below and do the same for other problems.
/*
Write a function named convertToString that accepts a number and converts the number into a string (hint: use String()) and return the converted value.
Do the following for the given problem:
*/
//1. Write a Function Decleration
function convertToString(n) {
return String(n);
}
//2. Write a Function Expression
let convertToString = function (n) {
return String(n);
};
//3. Write an Arrow Function
let convertToString = (n) => String(n);
//4. Write an Arrow Function with curly brackets (if possible)
let convertToString = (n) => {
return String(n);
};
//5. Execute the function
convertToString(21);
//6. Execute the function and store the return value in a variable.
let returnValue = convertToString(23);
//7. What is the typeof returnValue
// typeof returnValue is "string"
/* 1. Add one to the given value
Write a function named `addOne` that accepts a number, adds one to the given value and`return` it(hint: use return keyword).
Do the following with above problem.
*/
// 1. Write a Function Declaration
function addOne(num){
return num + 1;
}
// 2. Write a Function Expression
const addOne = function(num) {
return num + 1;
};
// 3. Write an Arrow Function
const addOne = (num) => num + 1;
// 4. Write an Arrow Function with curly brackets(if possible)
const addOne = (num) => {
return num + 1;
};
// 5. Execute the function
addOne();
// 6. Execute the function and store the return value in a variable.
let returnValue = addOne();
// 7. What is the typeof returnValue
typeof retuenValue is Number;
/* 2. Subtract one
Write a function named `substractOne` that accepts a number, substracts one from the given value and return it. (hint: use return keyword)
Do the following with above problem.
*/
// 1. Write a Function Declaration
function substractOne(num){
return num - 1;
}
// 2. Write a Function Expression
const substractOne = function(num) {
return num - 1;
};
// 3. Write an Arrow Function
const substractOne = num => num - 1;
// 4. Write an Arrow Function with curly brackets(if possible)
const substractOne = (num) => {
return num - 1;
};
// 5. Execute the function
substractOne();
// 6. Execute the function and store the return value in a variable.
let returnValue = substractOne();
// 7. What is the typeof returnValue
typeof retuenValue is Number;
/* 3. Add two numbers
Write a function named `sum` that accepts two numbers and returns the sum of them.
Do the following with above problem.
*/
// 1. Write a Function Declaration
function sum(numA, numB){
return numA + numB;
}
// 2. Write a Function Expression
const sum = function(numA, numB) {
return numA + numB;
};
// 3. Write an Arrow Function
const sum = numA, numB => numA + numB;
// 4. Write an Arrow Function with curly brackets(if possible)
const sum = (numA, numB) => {
return numA + numB;
};
// 5. Execute the function
sum(20,12);
// 6. Execute the function and store the return value in a variable
let returnValue = sum(20,10);
// 7. What is the typeof returnValue
typeof retuenValue is Number;
/* 4. Square of the given value
Write a function named `square` that accepts a number and returns the square of the number.
Do the following with above problem.
*/
// 1. Write a Function Declaration
function square(num){
return num * num;
}
// 2. Write a Function Expression
const square = function(num) {
return num * num;
};
// 3. Write an Arrow Function
const square = num => num * num;
// 4. Write an Arrow Function with curly brackets(if possible)
const square = (num) => {
return num * num;
};
// 5. Execute the function
square(20,12);
// 6. Execute the function and store the return value in a variable.
let returnValue = square(20,10);
// 7. What is the typeof returnValue
typeof retuenValue is Number;
/* 5. Find isGreater
Write a function named isGreater that accepts two numbers`x` and`y` and returns`true` if x is greater than y or return `false`.
Do the following with above problem.
*/
// 1. Write a Function Declaration
function isGreater(x,y){
if (x > y){
return `true`
} else{
return `false`
}
};
// 2. Write a Function Expression
const isGreater = function(x,y) {
return (x > y) ? true : false;
}
// 3. Write an Arrow Function
const isGreater = x,y => (x > y) ? true : false;
// 4. Write an Arrow Function with curly brackets(if possible)
const isGreater = x,y => {
return (x > y) ? true : false;
}
// 5. Execute the function
isGreater(20,12);
// 6. Execute the function and store the return value in a variable.
let returnValue = isGreater(20,10);
// 7. What is the typeof returnValue
typeof retuenValue is Boolean;
/* 6.
Write a function named `oddOrEven` that accepts a`number` and returns
- `Number is odd` for odd number
- `Number is even` for even number.
Do the following with above problem.
*/
// 1. Write a Function Declaration
function oddOrEven(n) {
if (n % 2 === 0) {
return `even`;
} else {
return `odd`;
}
}
// 2. Write an anonymous Function Expression
const oddOrEven = function(n) {
if (n % 2 === 0) {
return `even`;
} else {
return `odd`;
}
};
// 3. Write an named Function Expression
const oddOrEven = function isOdd(n) {
if (n % 2 === 0) {
return `even`;
} else {
return `odd`;
}
};
// 4. Write an Arrow Function
const oddOrEven = n => (n % 2 === 0) ? `even` : `odd`;
// 5. Write an Arrow Function with curly brackets(if possible)
const oddOrEven = (n) => {
if (n % 2 === 0) {
return `even`
} else {
return `odd`
}
}
// 6. Execute the function
oddOrEven();
// 7. Execute the function and store the return value in a variable.
let returnValue = oddOrEven(23,21);
// 8. What is the typeof returnValue
// typeof retuenValue is Number