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
9cd51a2
commit 88048f2
Showing
1 changed file
with
40 additions
and
0 deletions.
There are no files selected for viewing
40 changes: 40 additions & 0 deletions
40
Retos/Reto #14 - OCTAL Y HEXADECIMAL [Fácil]/javascript/giovanyosorio.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,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)); |