Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reto #14 - Javascript #2882

Merged
merged 1 commit into from
Apr 4, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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