|
1 | | -package com.mouredev.weeklychallenge2022 |
2 | | - |
3 | 1 | /* |
4 | 2 | * Reto #43 |
5 | 3 | * TRUCO O TRATO |
@@ -36,3 +34,90 @@ package com.mouredev.weeklychallenge2022 |
36 | 34 | * |
37 | 35 | */ |
38 | 36 |
|
| 37 | +const scares = ['🎃', '👻', '💀', '🕷', '🕸', '🦇']; |
| 38 | +const sweets = ['🍰', '🍬', '🍡', '🍭', '🍪', '🍫', '🧁', '🍩']; |
| 39 | + |
| 40 | +function getRandomIcons(number, icons) { |
| 41 | + let result = []; |
| 42 | + |
| 43 | + for (let i = 0; i < number; i++) { |
| 44 | + const icon = icons[Math.floor(Math.random() * icons.length)]; |
| 45 | + result.push(icon); |
| 46 | + } |
| 47 | + |
| 48 | + return result; |
| 49 | +} |
| 50 | + |
| 51 | +function getScares(people) { |
| 52 | + let numberScares = 0; |
| 53 | + let totalHeight = 0; |
| 54 | + |
| 55 | + people.forEach(person => { |
| 56 | + numberScares += Math.floor(person.nombre.length / 2); |
| 57 | + |
| 58 | + if (person.edad % 2 === 0) { |
| 59 | + numberScares += 2; |
| 60 | + } |
| 61 | + |
| 62 | + totalHeight += person.altura; |
| 63 | + }); |
| 64 | + |
| 65 | + numberScares += (totalHeight / 100) * 3; |
| 66 | + |
| 67 | + return getRandomIcons(numberScares, scares); |
| 68 | +} |
| 69 | + |
| 70 | +function getSweets(people) { |
| 71 | + const totalSweets = people.reduce((acc, person) => { |
| 72 | + acc += person.nombre.length; |
| 73 | + acc += person.edad > 10 ? 3 : Math.floor(person.edad / 3); |
| 74 | + acc += person.altura > 150 ? 6 : Math.floor(person.altura / 50) * 2; |
| 75 | + |
| 76 | + return acc; |
| 77 | + }, 0); |
| 78 | + |
| 79 | + return getRandomIcons(totalSweets, sweets); |
| 80 | +} |
| 81 | + |
| 82 | +function trickOrTreating(option, people = []) { |
| 83 | + const options = { |
| 84 | + truco: getScares, |
| 85 | + trato: getSweets, |
| 86 | + default: () => 'Error, opción incorrecta. Elija truco o trato' |
| 87 | + }; |
| 88 | + |
| 89 | + return (options[option] || options['default'])(people); |
| 90 | +} |
| 91 | + |
| 92 | + |
| 93 | + |
| 94 | + |
| 95 | + |
| 96 | + |
| 97 | + |
| 98 | + |
| 99 | +//Ejemplos |
| 100 | + |
| 101 | +const personas = [ |
| 102 | + { nombre: 'Ramón', edad: 20, altura: 180 }, |
| 103 | + { nombre: 'Sara', edad: 18, altura: 165 } |
| 104 | +]; |
| 105 | + |
| 106 | +console.log(trickOrTreating('truco', personas)); |
| 107 | +// [ |
| 108 | +// '💀', '🕸', '👻', '🕷', |
| 109 | +// '💀', '🕸', '🎃', '💀', |
| 110 | +// '🎃', '💀', '🕷', '👻', |
| 111 | +// '👻', '👻', '🕸', '🎃', |
| 112 | +// '🎃', '🎃', '👻' |
| 113 | +// ] |
| 114 | +console.log(trickOrTreating('trato', personas)); |
| 115 | +// [ |
| 116 | +// '🍩', '🍫', '🍪', '🍰', '🍭', |
| 117 | +// '🧁', '🍪', '🍪', '🍫', '🍰', |
| 118 | +// '🧁', '🍪', '🍩', '🍩', '🍩', |
| 119 | +// '🧁', '🍭', '🍬', '🍬', '🍬', |
| 120 | +// '🍬', '🍫', '🍪', '🍬', '🧁', |
| 121 | +// '🍬', '🍪' |
| 122 | +// ] |
| 123 | +console.log(trickOrTreating('', personas)); //Error, opción incorrecta. Elija truco o trato |
0 commit comments