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 #2881

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,78 @@
/*

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.

*/

function decimalOctalHexadecimal(numero) {
let arrayOctal = [];
let arrayHexadecimal = [];
let octal = "";
let hexadecimal = "";
let division = 0;
let residuo = 0;

let numeroAOctal = numero;
// regresa el numero en el sistma octal
while (numeroAOctal !== 0) {
division = Math.floor(numeroAOctal / 8);
residuo = numeroAOctal % 8;
arrayOctal.push(residuo);
numeroAOctal = division;
}
octal = arrayOctal.reverse().join("");

let numeroAHexadecimal = numero;
// regresa el numero en el sistema hexadecimal
while (numeroAHexadecimal !== 0) {
division = Math.floor(numeroAHexadecimal / 16);
residuo = numeroAHexadecimal % 16;
switch (residuo) {
case 10:
arrayHexadecimal.push("A");
break;
case 11:
arrayHexadecimal.push("B");
break;
case 12:
arrayHexadecimal.push("C");
break;
case 13:
arrayHexadecimal.push("D");
break;
case 14:
arrayHexadecimal.push("E");
break;
case 15:
arrayHexadecimal.push("F");
break;
default:
arrayHexadecimal.push(residuo);
break;
}
numeroAHexadecimal = division;
}

hexadecimal = arrayHexadecimal.reverse().join("");

// Hexadecimal validacion

if (hexadecimal.match(/[A-F]/)) {
hexadecimal = String(hexadecimal);
} else {
hexadecimal = Number(hexadecimal);
}

return {
"sistema octal": Number(octal),
"sistema hexadecimal": hexadecimal,
};
}

console.log(decimalOctalHexadecimal(8000)); // { 'sistema octal': 17500, 'sistema hexadecimal': '1F40' }
console.log(decimalOctalHexadecimal(1500)); // { 'sistema octal': 2734, 'sistema hexadecimal': '5DC' }
console.log(decimalOctalHexadecimal(500)); // { 'sistema octal': 764, 'sistema hexadecimal': '1F4' }
console.log(decimalOctalHexadecimal(80)); // { 'sistema octal': 120, 'sistema hexadecimal': 50 }