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

162
Visualizações
Increase and reduce index withing a fixed circular range in JS

I have a circular range of ascii values of a-z, from 97 to 122.
I am having trouble with decreasing the value, suppose the current value is 100 and i want to reduce it by 4 and it should return 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 Respostas
Responde à pergunta

0

Modulo will walk your modulo-class as you wish, if you just keep adding your

startIndex + (currentIndex%26)

Now, you just make sure, your currentIndex want go below 0

you modify only the 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 Relatório

0

The solution @thst gave works well. In short I'll mention my usage of the code

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 is a mysterious number (there is no logic to it) my friend came up with to get the decrement expression he made work.

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