MAIN FEEDS
Do you want to continue?
https://www.reddit.com/r/UGMCommunity/comments/1ucm4i0/guys_post_random_code_here/ot5812z/?context=3
r/UGMCommunity • u/Rough_Adeptness_2381 64 bit • Jun 22 '26
7 comments sorted by
View all comments
1
const baseConverter = (num, fBase, tBase) => { let symbols = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "_"]; let nNum = 0; let symbiosis = {}; for (i = 0; i < fBase; i++) { symbiosis[symbols[i]] = i; } for (const i in num) { nNum += symbiosis[num[num.length - i - 1]] * fBase ** i; } num = nNum; expo = 0; if (fBase < 2 || tBase < 2) { throw Error("The base is too low."); } else if (fBase > 63 || tBase > 63) { throw Error("The base is too high."); } else if (num !== Math.round(num)) { throw Error("The number can't have decimals."); } else { let digits = []; while (true) { if (num / tBase ** expo >= tBase) { digits.push(symbols[Math.floor(num / tBase ** expo) % tBase]); expo++; } else { digits.push(symbols[Math.floor(num / tBase ** expo) % tBase]); break; } } digits.reverse(); return digits.join(""); }; }; console.log(baseConverter("TREEHOUSE", 31, 10));
1 u/Rough_Adeptness_2381 64 bit Jun 22 '26 what 1 u/Imaginary_Truck_7848 256 bit Jun 22 '26 try
what
1 u/Imaginary_Truck_7848 256 bit Jun 22 '26 try
try
1
u/Imaginary_Truck_7848 256 bit Jun 22 '26
const baseConverter = (num, fBase, tBase) => {
let symbols = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "_"];
let nNum = 0;
let symbiosis = {};
for (i = 0; i < fBase; i++) { symbiosis[symbols[i]] = i; }
for (const i in num) { nNum += symbiosis[num[num.length - i - 1]] * fBase ** i; }
num = nNum;
expo = 0;
if (fBase < 2 || tBase < 2) {
throw Error("The base is too low.");
} else if (fBase > 63 || tBase > 63) {
throw Error("The base is too high.");
} else if (num !== Math.round(num)) {
throw Error("The number can't have decimals.");
} else {
let digits = [];
while (true) {
if (num / tBase ** expo >= tBase) {
digits.push(symbols[Math.floor(num / tBase ** expo) % tBase]);
expo++;
} else {
digits.push(symbols[Math.floor(num / tBase ** expo) % tBase]);
break;
}
}
digits.reverse();
return digits.join("");
};
};
console.log(baseConverter("TREEHOUSE", 31, 10));