Skip to content

Commit 581628d

Browse files
committed
mouredev#13 - Javascript
1 parent 83020fb commit 581628d

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* #13 PRUEBAS UNITARIAS
3+
*/
4+
5+
const sum = (num1, num2) => num1 + num2
6+
7+
// test
8+
const result1 = sum(2, 3)
9+
if (result1 === 5) {
10+
console.log('Test passed')
11+
} else {
12+
console.error('Error: The sum of 2 and 3 is 5')
13+
}
14+
15+
/*
16+
* DIFICULTAD EXTRA
17+
*/
18+
19+
const person = {
20+
name: 'cesar-ch',
21+
age: 3,
22+
birthDate: '2021-02-03',
23+
programmingLanguages: ["Javascript", "Python", "Java"]
24+
}
25+
26+
// test for existence of fields
27+
const expectedKeys = ["name", "age", "birthDate", "programmingLanguages"]
28+
const allKeysExist = expectedKeys.every(key => key in person)
29+
if (allKeysExist) {
30+
console.log('Test passed')
31+
} else {
32+
console.error('Error: The person object has the expected keys')
33+
}
34+
35+
// test to verify field data
36+
if (typeof person.name !== "string") {
37+
console.error('Error: The name field is not string')
38+
}
39+
40+
if (isNaN(person.age) || person.age < 0) {
41+
console.error('Error: Age must be a positive number')
42+
}
43+
44+
const regexDate = /^\d{4}-\d{2}-\d{2}$/
45+
46+
if (!regexDate.test(person.birthDate)) {
47+
console.error('Error: Birth date is not in the expected format')
48+
}
49+
50+
if (!Array.isArray(person.programmingLanguages)) {
51+
console.error('Error: Programming languages is not an array')
52+
}

0 commit comments

Comments
 (0)