I am currently trying to write a part of my code that takes a large number, say, 1e31, and converts it to 1no where no is a units measure. I currently have my code set up as such:
function getShortened(number) {
if (number <= 0) return number;
var num = Math.pow(10, Math.floor(Math.log10(number)) % 3) * (number / Math.pow(10, Math.floor(Math.log10(number))));
return num;
}
function getNotation(num) {
let firstArray = ["", "un", "du", "tr", "qa", "qi", "sx", "sp", "oc", "no"];
let secondArray = ["", "Du", "Tr", "Qa", "Qi", "Sx", "Sp", "Oc", "No"];
let output = "";
console.log((Math.floor(Math.log10(num) / 3)) + " " + (Math.floor(Math.log10(num) / 3) % 11 - 1) + " " + (Math.floor(Math.floor(Math.log10(num) / 3) / 11)));
output += firstArray[Math.floor(Math.log10(num) / 3) % 11 - 1];
output += secondArray[Math.floor(Math.floor(Math.log10(num) / 3) / 11)];
return output;
}
console.log(Math.round(getShortened(1e33)) + getNotation(1e33)); // should show 1Du
console.log(Math.round(getShortened(1e36)) + getNotation(1e36)); // should show 1unDu
console.log(Math.round(getShortened(1e30)) + getNotation(1e30)); // should show 1no
The issue that I am running into is that the index I use for firstArray works fine until it hits 9, then it jumps to -1. If anyone could give me some help that would be greatly appreciated. Let me know if you need any more of the code.
So I ended up figuring it out. The original code took a large number and essentially determined how many digits were in the number, then it would divide that by 3 in order to determine the index of an array that would be referenced for the value. When I attempted to transfer that from one large array to two smaller arrays that were more dynamic and in theory would allow for numbers larger than the limit in javascript (though that is a whole other issue). In order to do this, I would need to change the starting index into ones that cycle through the two arrays. The issue that I ran into was that there are special cases for indexes 0-4, and the 10 number repeating segment started at index 11. Essentially while I did account for the 1 index shift, I did it after I adjusted the original index and in doing so, tried to map indexes that are outside of the array. To fix this, it was surprisingly simple, I just account for the shift first and then the adjustment works perfectly.
function getNotation(num){
let firstArray = ["", "un", "du", "tr", "qa", "qi", "sx", "sp", "oc", "no"];
let secondArray = ["", "De", "Tr", "Qa", "Qi", "Sx", "Sp", "Oc", "No"];
let output = "";
console.log((Math.floor(Math.log10(num) / 3)) + " " + (Math.floor(Math.log10(num) / 3) % 11 - 1) + " " + (Math.floor(Math.floor(Math.log10(num) / 3) / 11)));
output += firstArray[(Math.floor(Math.log10(num) / 3) - 1) % 10];
output += secondArray[Math.floor(Math.floor(Math.log10(num) / 3) / 9)];
return output;
}