Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

164
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda