|
| 1 | +/* |
| 2 | + * EJERCICIO: |
| 3 | + * Crea una función que se encargue de sumar dos números y retornar |
| 4 | + * su resultado. |
| 5 | + * Crea un test, utilizando las herramientas de tu lenguaje, que sea |
| 6 | + * capaz de determinar si esa función se ejecuta correctamente. |
| 7 | + * |
| 8 | + * DIFICULTAD EXTRA (opcional): |
| 9 | + * Crea un diccionario con las siguientes claves y valores: |
| 10 | + * "name": "Tu nombre" |
| 11 | + * "age": "Tu edad" |
| 12 | + * "birth_date": "Tu fecha de nacimiento" |
| 13 | + * "programming_languages": ["Listado de lenguajes de programación"] |
| 14 | + * Crea dos test: |
| 15 | + * - Un primero que determine que existen todos los campos. |
| 16 | + * - Un segundo que determine que los datos introducidos son correctos. |
| 17 | + */ |
| 18 | + |
| 19 | +function sum(a, b) { |
| 20 | + return a + b; |
| 21 | +} |
| 22 | + |
| 23 | +function sumTest() { |
| 24 | + const resultado = sum(3, 5); |
| 25 | + const esperado = 8; |
| 26 | + |
| 27 | + if (resultado !== esperado) { |
| 28 | + throw new Error(`Error: ${resultado} es distinto de ${esperado}`); |
| 29 | + } else { |
| 30 | + console.log("!Test OK"); |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +try { |
| 35 | + sumTest(); |
| 36 | +} catch (error) { |
| 37 | + console.error(error.message); |
| 38 | +} |
| 39 | + |
| 40 | +const user = { |
| 41 | + name: "Jose", |
| 42 | + age: 25, |
| 43 | + birth_date: "05/11/98", |
| 44 | + programming_languages: ["javaScript", "Python", "Go", "C#"], |
| 45 | +}; |
| 46 | + |
| 47 | +function userTest() { |
| 48 | + const userDate = ["name", "age", "birth_date", "programming_languages"]; |
| 49 | + |
| 50 | + userDate.forEach((element) => { |
| 51 | + if (!(element in user)) { |
| 52 | + throw new Error(`Error: El campo ${element} no existe en el objeto user`); |
| 53 | + } |
| 54 | + |
| 55 | + console.log("Todos los campos existen en el objeto user!"); |
| 56 | + }); |
| 57 | +} |
| 58 | + |
| 59 | +function dataTest() { |
| 60 | + if (typeof user.name !== "string" || user.name.trim() === "") { |
| 61 | + throw new Error( |
| 62 | + `Error: el campo ${user.name} deber ser un string no vacio.` |
| 63 | + ); |
| 64 | + } |
| 65 | + |
| 66 | + if (typeof user.age !== "number" || user.age < 0) { |
| 67 | + throw new Error(`Error: el campo ${user.age} debe ser un numero valido`); |
| 68 | + } |
| 69 | + |
| 70 | + if (typeof user.birth_date !== "string" || user.name.trim() === "") { |
| 71 | + throw new Error( |
| 72 | + `Error: el campo ${user.birth_date} deber ser un string no vacio.` |
| 73 | + ); |
| 74 | + } |
| 75 | + |
| 76 | + if (!Array.isArray(user.programming_languages)) { |
| 77 | + throw new Error( |
| 78 | + `Error: el campo ${user.programming_languages} debe ser un array.` |
| 79 | + ); |
| 80 | + } |
| 81 | + |
| 82 | + user.programming_languages.forEach((language) => { |
| 83 | + if (typeof language !== "string") { |
| 84 | + throw new Error( |
| 85 | + `Error: Cada lenguaje en ${user.programming_languages} debe ser un string` |
| 86 | + ); |
| 87 | + } |
| 88 | + }); |
| 89 | + |
| 90 | + console.log("Datos correctos"); |
| 91 | +} |
| 92 | + |
| 93 | +try { |
| 94 | + userTest(); |
| 95 | + dataTest(); |
| 96 | +} catch (err) { |
| 97 | + console.error(err.message); |
| 98 | +} |
0 commit comments