From f28e8c5dc02a6ea92116a423afa9b8d08723f66f Mon Sep 17 00:00:00 2001 From: jmedinac1987 Date: Mon, 24 Jul 2023 19:57:15 -0500 Subject: [PATCH 1/2] Reto #30 - JavaScript --- .../javascript/jmedinac1987.js | 62 +++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 Retos/Reto #30 - EL TECLADO T9 [Media]/javascript/jmedinac1987.js diff --git a/Retos/Reto #30 - EL TECLADO T9 [Media]/javascript/jmedinac1987.js b/Retos/Reto #30 - EL TECLADO T9 [Media]/javascript/jmedinac1987.js new file mode 100644 index 0000000000..4c428118e8 --- /dev/null +++ b/Retos/Reto #30 - EL TECLADO T9 [Media]/javascript/jmedinac1987.js @@ -0,0 +1,62 @@ +function validateNumberBlock(numberBlock) { + if (numberBlock.length == 0) return false; + + let regex = /^[0-9-]+$/; + let theNumberBlockHasOnlyNumbersAndHyphens = regex.test(numberBlock); + + if (!theNumberBlockHasOnlyNumbersAndHyphens) return false; + + let arrayNumberBlock = []; + let isNumberBlockValid = true; + + arrayNumberBlock = numberBlock.trim().split("-"); + + arrayNumberBlock.some((element) => { + let arrayElement = element.split(""); + if (arrayElement.length >= 2) { + for (let i = 0; i < arrayElement.length; i++) { + if (typeof arrayElement[i + 1] === "undefined") { + if (arrayElement[i] != arrayElement[i - 1]) { + isNumberBlockValid = false; + break; + } + } else if (arrayElement[i] != arrayElement[i + 1]) { + isNumberBlockValid = false; + break; + } + } + } + }); + + return { TrueOrFalse: isNumberBlockValid, ArrayTest: arrayNumberBlock }; +} + +function decodeMessage(numberBlock) { + let responseValidateNumberBlock = validateNumberBlock(numberBlock); + if (!responseValidateNumberBlock.TrueOrFalse) + return "We're sorry, but your number block is invalid"; + + let message = ""; + const decode = { + 2: "a", 22: "b", 222: "c", + 3: "d", 33: "e", 333: "f", + 4: "g", 44: "h", 444: "i", + 5: "j", 55: "k", 555: "l", + 6: "m", 66: "n", 666: "o", + 7: "p", 77: "q", 777: "r", 7777: "s", + 8: "t", 88: "u", 888: "v", + 9: "w", 99: "x", 999: "y", 9999: "z", + }; + + responseValidateNumberBlock.ArrayTest.forEach((element) => { + if (typeof decode[element] === "undefined"){ + message += " "; + } else { + message += decode[element]; + } + }); + + return message; +} + +console.log(decodeMessage("6-666-88-777-33-3-33-888")); From f7929a7755b2feff8f71049cb496666f853bd750 Mon Sep 17 00:00:00 2001 From: jmedinac1987 Date: Mon, 24 Jul 2023 20:04:31 -0500 Subject: [PATCH 2/2] Reto #30 - JavaScript ajuste --- .../javascript/jmedinac1987.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Retos/Reto #30 - EL TECLADO T9 [Media]/javascript/jmedinac1987.js b/Retos/Reto #30 - EL TECLADO T9 [Media]/javascript/jmedinac1987.js index 4c428118e8..1fa43eee06 100644 --- a/Retos/Reto #30 - EL TECLADO T9 [Media]/javascript/jmedinac1987.js +++ b/Retos/Reto #30 - EL TECLADO T9 [Media]/javascript/jmedinac1987.js @@ -11,7 +11,7 @@ function validateNumberBlock(numberBlock) { arrayNumberBlock = numberBlock.trim().split("-"); - arrayNumberBlock.some((element) => { + arrayNumberBlock.forEach((element) => { let arrayElement = element.split(""); if (arrayElement.length >= 2) { for (let i = 0; i < arrayElement.length; i++) { @@ -56,7 +56,7 @@ function decodeMessage(numberBlock) { } }); - return message; + return message.toUpperCase(); } console.log(decodeMessage("6-666-88-777-33-3-33-888"));