-
Notifications
You must be signed in to change notification settings - Fork 0
/
02-week-js-practice.test.js
292 lines (253 loc) · 10.2 KB
/
02-week-js-practice.test.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
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
console.log("=================================")
console.log("CHALLENG NUMER 3")
console.log("=================================")
// describe('makeItCool', () => {
// it ('check the maksimum equal number', () => {
// Test.assertEquals(diff(['23-32','32-23','2-6','98-98','100-101']), '23-32');
// Test.assertEquals(diff(['22-22','56-56']), false);
// Test.assertEquals(diff(['52542-522','0-1000000']),'0-1000000');
// })
// })
// const a = ['23-32','32-23','2-6','98-98','100-101']
// const b = ['22-22','56-56']
// const c = ['52542-522','0-1000000']
// const makeItCool = (str) => {
// // console.log("str:", str);
// let newArr = []
// str.map((value, index, array) => {
// // let newValue = value.spit('-')
// // console.log("v:", value, "i:", index, "a:", array);
// let newValue = value.split('-')
// let difference = newValue[0] - newValue[1]
// // console.log("difference", difference);
// // console.log("newValue:", newValue);
// if (difference > Math.max.apply(difference)){
// // console.log(value);
// return newArr.push(difference)
// } else {
// return false
// }
// })
// return newArr
// }
function makeItCool(str){
let greatestDiff = 0;
let diffValues = '';
str.map(string => {
const splitString = string.split('-');
const absoluteDifference = Math.abs(splitString[0] - splitString[1]);
if (absoluteDifference > greatestDiff) {
diffValues = splitString.join('-');
greatestDiff = absoluteDifference;
}
})
if (greatestDiff === 0) {
return false
} else {
return diffValues;
}
}
// console.log("a:", makeItCool(a));
// console.log("b:", makeItCool(b));
// console.log("c:", makeItCool(c));
// Task: Find the number couple with the greatest difference from a list of number-couples.
// Input: A list of number-couples, where each couple is represented as a string containing two positive integers separated by a hyphen ("-").
// Output: The number couple with the greatest difference, or False if there is no difference among any of the couples or in case of an empty list.
// Additional Information:
// All number couples will be given as strings.
// All numbers in the couples are positive integers.
// If multiple couples have the same greatest difference, return the first one encountered in the input list.
// If there is no difference (both numbers in a couple are equal), return False.
// 💻 Challenges
// 24 Write the test for a function that returns "drink coffee" if you are tired and "keep working" if you are not tired.
describe('keepItComing', () => {
it ('checks if you are tired or not', () => {
expect(keepItComing("tired")).toEqual("keep drinking coffee")
expect(keepItComing()).toEqual("keep working")
})
})
// Create the function that will make the test pass.
const keepItComing = (mood) => {
if (mood === "tired"){
return "keep drinking coffee"
} else { return "keep working"}
}
console.log("24a:", keepItComing('tired'));
console.log("24b:", keepItComing('bo bo ga ga'));
// 25 Write the test for a function that returns "relax" if you are stressed and "keep going" if you are not stressed.
describe('keepCalmAnd', () => {
it ('checks if you are stressed or not', () => {
expect(keepCalmAnd("stressed")).toEqual("relax")
expect(keepCalmAnd()).toEqual("keep going")
})
})
// Create the function that will make the test pass.
const keepCalmAnd = (mood) => {
if (mood === "stressed"){
return "relax"
} else { return "keep going"}
}
console.log("25a:", keepCalmAnd('stressed'));
console.log("25b:", keepCalmAnd('bo bo ga ga'));
// 26 Write the test for a function that returns "in budget" if a price is lower than $300.
describe('priceIsRight', (money) => {
it ('checks if the price is in budget', () => {
expect(priceIsRight(money < 300)).toEqual("in budget")
expect(priceIsRight(money)).toEqual("not in budget")
})
})
// Create the function that will make the test pass.
const priceIsRight = (money) => {
if (money < 300){
return "in budget"
} else { return "not in budget"}
}
// console.log("26a:", priceIsRight(250));
// console.log("26b:", priceIsRight(350));
// 27 Write the test for a function that takes in two numbers and returns the smaller number.
describe('takeInNum', (num1, num2) => {
it ('check the smallest number', () => {
expect(takeInNum(num1 < num2)).toEqual(num1)
expect(takeInNum(num1 > num2)).toEqual(num2)
})
})
// Create the function that will make the test pass.
const takeInNum = (num1, num2) => {
if (num1 < num2){
return num1
} else { return num2}
}
// 28 Write the test for a function that takes in one numbers and returns whether the number is odd.
describe('oddTodd', () => {
it ('check if the number is odd as Todd', () => {
expect(oddTodd(3)).toEqual("well, that's odd")
expect(oddTodd(2)).toEqual("that ain't odd")
})
})
// Create the function that will make the test pass.
const oddTodd = (num) => {
if (num % 2 === 1){
return "well, that's odd"
} else { return "that ain't odd"}
}
// 29 Write the test for a function that takes in a fruit and returns "yellow" if the argument is banana, "red" if apple and "purple" if grape.
describe('fruitColor', () => {
it ('returns "yellow" if the argument is banana, "red" if apple and "purple" if grape.', () => {
expect(fruitColor('banana')).toEqual("yellow")
expect(fruitColor('apple')).toEqual("red")
expect(fruitColor('grape')).toEqual("purple")
expect(fruitColor("uyuy")).toEqual("what?")
})
})
// Create the function that will make the test pass.
const fruitColor = (value) => {
if (value === 'banana'){
return "yellow"
} else if (value === 'apple') {
return "red"
} else if (value === 'grape') {
return "purple"
} else {return "what?"}
}
// 30 Write the test for a function called rick that returns "Morty".
describe('rickAndMorty', () => {
it ('called rick that returns "Morty"', () => {
expect(rickAndMorty('Rick')).toEqual("Morty")
expect(rickAndMorty('Morty')).toEqual("Rick")
})
})
// Create the function that will make the test pass.
const rickAndMorty = (value) => {
if (value === 'Rick'){
return "Morty"
} else { return "Rick"}
}
// 31 Write the test for a function called greeter that takes a name as an argument and returns a greeting with that name to the screen.
describe('greeter', () => {
it ('takes a name as an argument and returns a greeting with that name to the screen.', () => {
expect(greeter('Xena')).toEqual(`Ahoy there Xena`)
// expect(greeter()).toEqual("nope")
})
})
// Create the function that will make the test pass.
const greeter = (name) => {
if (name === `${name}`){
return `Ahoy there ${name}`
} else {
return "nope"
}
}
// 32 Write the test for a function called oddOrEven that takes a number as an argument and logs whether the number is odd or even.
describe('oddOrEven', () => {
it ('takes a number as an argument and logs whether the number is odd or even.', () => {
expect(oddOrEven(3)).toEqual('odd')
expect(oddOrEven(4)).toEqual('even')
})
})
// Create the function that will make the test pass.
const oddOrEven = (num) => {
if (num % 2 === 1){
return "odd"
} else if (num % 2 === 0){
return "even"
} else {return "nope"}
}
// 33 Write the test for a function called doubler that takes a number and returns the result of the number multiplied by 2.
describe('doubler', () => {
it ('takes a number and returns the result of the number multiplied by 2.', () => {
expect(doubler(3)).toEqual(6)
})
})
// Create the function that will make the test pass.
const doubler = (value) => {
if (value){
return value * 2
} else {
return "nope"
}
}
// 34 Write the test for a function called multiply that takes two numbers as arguments and logs the result of one of the numbers multiplied by the other.
describe('multiply', () => {
it ('takes two numbers as arguments and logs the result of one of the numbers multiplied by the other.', () => {
expect(multiply(3, 2)).toEqual(6)
})
})
// Create the function that will make the test pass.
const multiply = (num1, num2) => {
if (num1 > 0 && num2 > 0 ){
return num1*num2
} else {
return "nope"
}
}
// 35 Write the test for a function called divisibleBy that takes two numbers as arguments and returns whether the first number is evenly divisible by the second so that divisibleBy(10, 5) logs "10 is evenly divisible by 5".
describe('divisibleBy', () => {
it ('takes two numbers as arguments and returns whether the first number is evenly divisible by the second so that divisibleBy(10, 5) logs "10 is evenly divisible by 5".', () => {
expect(divisibleBy(10, 5)).toEqual("10 is evenly divisible by 5")
expect(divisibleBy(10, 6)).toEqual("10 is not evenly divisible by 6")
})
})
// Create the function that will make the test pass.
const divisibleBy = (num1, num2) => {
if (num1 % num2 === 0){
return `${num1} is evenly divisible by ${num2}`
} else {
return `${num1} is not evenly divisible by ${num2}`
}
}
// 36 Write the test for a function called fizzbuzz. If a number is a multiple of 3, replace it with the word "fizz". If a number is a multiple of five, replace it with the word "buzz". If a number is a multiple of both 3 and 5, replace it with "fizzbuzz".
describe('fizzbuzz', () => {
it ('is a multiple of 3, replace it with the word "fizz". If a number is a multiple of five, replace it with the word "buzz". If a number is a multiple of both 3 and 5, replace it with "fizzbuzz".', () => {
expect(fizzbuzz(5)).toEqual("buzz")
expect(fizzbuzz(3)).toEqual("fizz")
expect(fizzbuzz(15)).toEqual("fizzbuz")
})
})
// Create the function that will make the test pass.
const fizzbuzz = (value) => {
if (value % 15 === 0){
return "fizzbuz"
} else if (value % 5 === 0) {
return "buzz"
} else { return "fizz"}
}