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

157
Views
Aumente y reduzca el índice dentro de un rango circular fijo en JS

Tengo un rango circular de valores ascii de az, de 97 a 122.
Tengo problemas para disminuir el valor, supongamos que el valor actual es 100 y quiero reducirlo en 4 y debería devolver 122.

 const startIndex=97 const endIndex=122 let increaseBy=reduceBy=5 let currentIndex=122 // increment // this works currentIndex=startIndex+((currentIndex-startIndex+increaseBy)%26) // decrement // need help currentIndex=attempted_something_that_you_will_find_it_as_dumb
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Modulo recorrerá su clase de módulo como desee, si sigue agregando su

startIndex + (currentIndex%26)

Ahora, solo asegúrese de que su índice actual quiera ir por debajo de 0

modificas solo el currentIndex

 let startIndex = 97; let currentIndex = 0; for(let inc = 0; inc < 10; inc++) { currentIndex += 5; console.log(startIndex + (currentIndex%26)); } for(let inc = 0; inc < 10; inc++) { currentIndex -= 5; console.log(startIndex + (currentIndex%26)); } console.log("index will go below zero -> will break"); for(let inc = 0; inc < 10; inc++) { currentIndex -= 5; console.log(startIndex + (currentIndex%26)); } console.log("index will stop at zero -> will not break"); for(let inc = 0; inc < 10; inc++) { currentIndex = Math.max(0, currentIndex - 5); console.log(startIndex + (currentIndex%26)); } console.log("index will reset to modulo at zero -> will not break"); for(let inc = 0; inc < 10; inc++) { currentIndex -= 5; currentIndex = (currentIndex < 0) ? (26 + currentIndex) : currentIndex; console.log(startIndex + (currentIndex%26)); }
about 4 years ago · Juan Pablo Isaza Report

0

La solución que dio @thst funciona bien. En resumen, mencionaré mi uso del código.

 const startIndex = 97; const endIndex = 122; let currentIndex = 122; //this value is varies according to the input i take let increaseBy = (reduceBy = 5); // increment currentIndex = startIndex + ((currentIndex - startIndex + increaseBy) % 26); // decrement const milansConst=7; currentIndex = startIndex + ((currentIndex - reduceBy + milansConst) % 26);

milansConst es un número misterioso ( no tiene lógica) que se le ocurrió a mi amigo para obtener la expresión de decremento que hizo funcionar.

about 4 years ago · Juan Pablo Isaza 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!