Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

170
Views
¿Obtener el nivel de usuario actual sin usar bucles while?

En mi aplicación de mensajería, tengo un sistema de niveles que rastrea la actividad de un usuario con XP. No almacena el nivel real, solo la cantidad de XP que tienen. Aquí está la función para calcular el nivel:

 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 }; }

La fórmula para calcular cuánto XP requiere cada nivel es 30 √(√(nivel)) . Sin embargo, no quiero usar un bucle while, porque la aplicación comienza a ralentizarse un poco después del nivel 60. ¿Hay alguna forma de obtener el nivel actual sin tener que usar bucles?

about 4 years ago · Santiago Gelvez
2 answers
Answer question

0

Puede reemplazar el bucle con

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

toda la funcion

 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 Report

0

Debe repensar cómo calcula el nivel y usar para ingresar algo como: cuánto tiempo ha estado activo el usuario. Aquí hay un código de ejemplo que usa una biblioteca llamada 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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!