|
| 1 | +//#47 - CALENDARIO DE ADVIENTO |
| 2 | +/* |
| 3 | + * EJERCICIO: |
| 4 | + * ¡Cada año celebramos el aDEViento! 24 días, 24 regalos para |
| 5 | + * developers. Del 1 al 24 de diciembre: https://adviento.dev |
| 6 | + * |
| 7 | + * Dibuja un calendario por terminal e implementa una |
| 8 | + * funcionalidad para seleccionar días y mostrar regalos. |
| 9 | + * - El calendario mostrará los días del 1 al 24 repartidos |
| 10 | + * en 6 columnas a modo de cuadrícula. |
| 11 | + * - Cada cuadrícula correspondiente a un día tendrá un tamaño |
| 12 | + * de 4x3 caracteres, y sus bordes serán asteríscos. |
| 13 | + * - Las cuadrículas dejarán un espacio entre ellas. |
| 14 | + * - En el medio de cada cuadrícula aparecerá el día entre el |
| 15 | + * 01 y el 24. |
| 16 | + * |
| 17 | + * Ejemplo de cuadrículas: |
| 18 | + * **** **** **** |
| 19 | + * *01* *02* *03* ... |
| 20 | + * **** **** **** |
| 21 | + * |
| 22 | + * - El usuario seleccioná qué día quiere descubrir. |
| 23 | + * - Si está sin descubrir, se le dirá que ha abierto ese día |
| 24 | + * y se mostrará de nuevo el calendario con esa cuadrícula |
| 25 | + * cubierta de asteríscos (sin mostrar el día). |
| 26 | + * |
| 27 | + * Ejemplo de selección del día 1 |
| 28 | + * **** **** **** |
| 29 | + * **** *02* *03* ... |
| 30 | + * **** **** **** |
| 31 | + * |
| 32 | + * - Si se selecciona un número ya descubierto, se le notifica |
| 33 | + * al usuario. |
| 34 | + */ |
| 35 | + |
| 36 | +let log = console.log; |
| 37 | + |
| 38 | +const readline = require('readline'); |
| 39 | +const rl = readline.createInterface({ |
| 40 | + input: process.stdin, |
| 41 | + output: process.stdout |
| 42 | +}); |
| 43 | + |
| 44 | +function drawCalendar(discoveredDays) { |
| 45 | + const COLUMNS_COUNT = 6; |
| 46 | + const CELL_WIDTH = 4; |
| 47 | + const CELL_HEIGHT = 3; |
| 48 | + |
| 49 | + let calendar = ''; |
| 50 | + |
| 51 | + for (let row = 0; row < CELL_HEIGHT; row++) { |
| 52 | + for (let day = 1; day <= 24; day++) { |
| 53 | + if (row === 1) { |
| 54 | + calendar += discoveredDays.includes(day) ? '*'.repeat(CELL_WIDTH) : `*${String(day).padStart(2, '0')}*`; |
| 55 | + } else { |
| 56 | + calendar += '*'.repeat(CELL_WIDTH); |
| 57 | + } |
| 58 | + if (day % COLUMNS_COUNT === 0) { |
| 59 | + calendar += '\n'; |
| 60 | + } else { |
| 61 | + calendar += ' '; |
| 62 | + } |
| 63 | + } |
| 64 | + calendar += '\n'; |
| 65 | + } |
| 66 | + |
| 67 | + return calendar; |
| 68 | +} |
| 69 | + |
| 70 | +function handleUserInput(discoveredDays) { |
| 71 | + return new Promise((resolve) => { |
| 72 | + rl.question('Enter the day you want to discover (1-24):', (input) => { |
| 73 | + const day = parseInt(input, 10); |
| 74 | + |
| 75 | + if (isNaN(day) || day < 1 || day > 24) { |
| 76 | + log('\nInvalid day. Please enter a number between 1 and 24.\n'); |
| 77 | + return resolve(discoveredDays); |
| 78 | + } |
| 79 | + |
| 80 | + if (discoveredDays.includes(day)) { |
| 81 | + log(`\nDay ${day} has already been discovered.\n`); |
| 82 | + return resolve(discoveredDays); |
| 83 | + } else { |
| 84 | + log(`\nGift of the day ${day}: ${prices[day - 1]} 🎁 `); |
| 85 | + } |
| 86 | + |
| 87 | + log(`\nYou have discovered day ${day}!\n`); |
| 88 | + return resolve([...discoveredDays, day]); |
| 89 | + }); |
| 90 | + }); |
| 91 | +} |
| 92 | + |
| 93 | +async function main() { |
| 94 | + let discoveredDays = []; |
| 95 | + |
| 96 | + while (discoveredDays.length < 24) { |
| 97 | + log('\n Adviento Calendar!\n'); |
| 98 | + log(drawCalendar(discoveredDays)); |
| 99 | + discoveredDays = await handleUserInput(discoveredDays); |
| 100 | + } |
| 101 | + |
| 102 | + log('\n Adviento Calendar!\n'); |
| 103 | + log(drawCalendar(discoveredDays)); |
| 104 | + log('Congratulations! You have discovered all 24 days.'); |
| 105 | + rl.close(); |
| 106 | +} |
| 107 | + |
| 108 | +const prices = [ |
| 109 | + "$29.99 and Basic Linux Terminal Usage course", |
| 110 | + "$39.99 and How to Perform Smart Commits? course", |
| 111 | + "$49.99 and Introduction to Programming Logic course", |
| 112 | + "$49.99 and Database Fundamentals course", |
| 113 | + "$49.99 and Best Practices in Python course", |
| 114 | + "$59.99 and Functional Programming Fundamentals course", |
| 115 | + "$59.99 and IoT: Current Applications course", |
| 116 | + "$69.99 and Software Design Patterns in Python course", |
| 117 | + "$69.99 and 0 to Hero in PostgreSQL course", |
| 118 | + "$79.99 and Qt4 Interface Development course", |
| 119 | + "$79.99 and Applying SOLID Patterns in Java course", |
| 120 | + "$79.99 and Go: The Language of the Future? course", |
| 121 | + "$89.99 and C# Development course", |
| 122 | + "$89.99 and Artificial Intelligence Fundamentals course", |
| 123 | + "$99.99 and Agile Methodologies Diploma course", |
| 124 | + "$99.99 and Django Rest Framework (DRF) Primer course", |
| 125 | + "$99.99 and Optimization Algorithms in C++ course", |
| 126 | + "$109.99 and Building REST Services with AWS API Gateway and Lambda course", |
| 127 | + "$119.99 and Data Science with Python and R course", |
| 128 | + "$79.99 and Object-Oriented Programming in Java course", |
| 129 | + "$89.99 and Traditional Methodologies Paradigm: Still Relevant? Applicable Cases course", |
| 130 | + "$69.99 and NumPy and Pandas in Python course", |
| 131 | + "$59.99 and Blockchain Fundamentals course", |
| 132 | + "$79.99 and Microservices Architecture with Spring Boot course", |
| 133 | + "$89.99 and Ethical Hacking and Cybersecurity Essentials course" |
| 134 | +]; |
| 135 | + |
| 136 | + |
| 137 | +main(); |
0 commit comments