-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2882 from marcode24/challenge-14
Reto #14 - Javascript
- Loading branch information
Showing
1 changed file
with
35 additions
and
0 deletions.
There are no files selected for viewing
35 changes: 35 additions & 0 deletions
35
Retos/Reto #14 - OCTAL Y HEXADECIMAL [Fácil]/javascript/marcode24.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,35 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
const convertToOctal = (number) => { | ||
let octal = ''; | ||
while (number > 0) { | ||
const rest = number % 8; | ||
octal = rest + octal; | ||
number = Math.floor(number / 8); | ||
} | ||
|
||
return octal; | ||
}; | ||
|
||
const convertToHexadecimal = (number) => { | ||
let hexadecimal = ''; | ||
while (number > 0) { | ||
const remainder = number % 16; | ||
const char = remainder < 10 ? remainder : String.fromCharCode(remainder + 55); | ||
hexadecimal = char + hexadecimal; | ||
number = Math.floor(number / 16); | ||
} | ||
return hexadecimal; | ||
}; | ||
|
||
const convertToHexadecimalAndOctal = (number) => ({ | ||
octal: convertToOctal(number), | ||
hexadecimal: convertToHexadecimal(number), | ||
}); | ||
|
||
// Visita mi repo en GitHub para ver y correr los tests de este código --> https://github.com/marcode24/weekly-challenges |