Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

163
Visualizações
Math for combining two array's values as notation for a large number

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.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

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;
}
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda