forked from mouredev/retos-programacion-2023
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4237f61
commit f28e8c5
Showing
1 changed file
with
62 additions
and
0 deletions.
There are no files selected for viewing
62 changes: 62 additions & 0 deletions
62
Retos/Reto #30 - EL TECLADO T9 [Media]/javascript/jmedinac1987.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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")); |