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

180
Vistas
Get current user level without using while loops?

In my messaging app, I have a level system that track's a user's activeness with XP. It doesn't store the actual level, only the amount of XP they have. Here's the function for calculating the level:

function calculateLevel(num) {
  var lv = 0;
  do {
    lv++;
  } while (num >= Math.floor(Math.pow(30, Math.sqrt(Math.sqrt(lv)))));
  var x = num-Math.floor(Math.pow(30, Math.sqrt(Math.sqrt(lv-1))));
  var left = Math.floor(Math.pow(30, Math.sqrt(Math.sqrt(lv))))-num;
  var thislv = x + left;
  return {
    level: lv,
    xp: x,
    left: left,
    next: Math.floor(Math.pow(30, Math.sqrt(Math.sqrt(lv)))),
    prev: Math.floor(Math.pow(30, Math.sqrt(Math.sqrt(lv-1)))),
    thislv: thislv+1
  };
}

The formula for calculating how much XP each level requires is 30√(√(level)). However, I don't want to use a while loop, because the app starts to slow down a bit after level 60. Is there a way to get the current level without having to use loops?

about 4 years ago · Santiago Gelvez
2 Respuestas
Responde la pregunta

0

You can replace the loop with

lv = Math.floor((Math.log(num)/Math.log(30))**4)

The whole function

function calculateLevel(num) {
  var lv = Math.floor((Math.log(num)/Math.log(30))**4);
  var x = num-Math.floor(Math.pow(30, Math.sqrt(Math.sqrt(lv-1))));
  var left = Math.floor(Math.pow(30, Math.sqrt(Math.sqrt(lv))))-num;
  var thislv = x + left;
  return {
    level: lv,
    xp: x,
    left: left,
    next: Math.floor(Math.pow(30, Math.sqrt(Math.sqrt(lv)))),
    prev: Math.floor(Math.pow(30, Math.sqrt(Math.sqrt(lv-1)))),
    thislv: thislv+1
  };
}
about 4 years ago · Santiago Gelvez Denunciar

0

You should rethink how you calculate level, and use for input something like: how long has user been active. Here's some example code using a library called RxJS.

import { fromEvent, merge } from "rxjs";
import { debounceTime } from "rxjs/operators";

function listenForActivity() {
    let lastChecked = Date.now()
    const activities$ = merge(
        ...[
            "mousemove",
            "mousedown",
            "touchstart",
            "keydown",
            "click",
            "scroll",
        ].map((ev) => fromEvent(document, ev)),
        fromEvent(window, "focus")
    );
    return activities$.pipe(debounceTime(2000)).subscribe(() => {
        let timeActive = Date.now() - lastChecked
        calculateLevel(timeActive)
    });
}
about 4 years ago · Santiago Gelvez 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