From 88048f29cca5020a936e79946910829aecd0fe23 Mon Sep 17 00:00:00 2001 From: Giovany Osorio <30839218+giovanyosorio@users.noreply.github.com> Date: Tue, 31 Oct 2023 23:30:24 +0000 Subject: [PATCH] Reto #14 - Javascript --- .../javascript/giovanyosorio.js" | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 "Retos/Reto #14 - OCTAL Y HEXADECIMAL [F\303\241cil]/javascript/giovanyosorio.js" diff --git "a/Retos/Reto #14 - OCTAL Y HEXADECIMAL [F\303\241cil]/javascript/giovanyosorio.js" "b/Retos/Reto #14 - OCTAL Y HEXADECIMAL [F\303\241cil]/javascript/giovanyosorio.js" new file mode 100644 index 0000000000..5f4f305461 --- /dev/null +++ "b/Retos/Reto #14 - OCTAL Y HEXADECIMAL [F\303\241cil]/javascript/giovanyosorio.js" @@ -0,0 +1,40 @@ +/* + * Crea una función que reciba un número decimal y lo trasforme a Octal + * y Hexadecimal. + * - No está permitido usar funciones propias del lenguaje de programación que + * realicen esas operaciones directamente. + */ +let hexas = { + 10: 'A', + 11: 'B', + 12: 'C', + 13: 'D', + 14: 'E', + 15: 'F' +} +function decimalToOctal(number) { + let octal = ""; + let result = number; + while (result > 0) { + octal = (result % 8) + octal; + result = Math.floor(result / 8); + } + return octal; +} + +function decimalToHexadecimal(number) { + let hexadecimal = ""; + let result = number; + while (result > 0) { + let temp = result % 16; + if (temp > 9) { + temp = hexas[temp]; + } + hexadecimal = temp + hexadecimal; + result = Math.floor(result / 16); + } + return hexadecimal; + } + +console.log(decimalToOctal(10)); +console.log(decimalToHexadecimal(16852));