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

209
Visualizações
How do I put a calculation inside a for loop with an if statement in between? What is the correct syntax?

With the use of my for loop looping backwards I want to get every other digit, double it and check if that number is bigger than 9. If the number is bigger than 9, I want to subtract nine from it. I want to do this in one function. Maybe use a built in function. This is what I got so far:

const validateCred = array => {
  for (let i = array.length -2; i >= 0; i = i -2 ) {
    let multiply = array[i] * 2;
    if (multiply > 9) {
      let minusNine = multiply -9;
    }
  }
};

Any suggestions would be a big help!

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

  • first You need an array to store numbers for iterated array res.
  • to iterate backwords you must start with array.length - 1 not -2 or you will lost last one.
  • to iterated over all numbers decrement the i with -1
const validateCred = array => {
  const res = [];
  for (let i = array.length -1; i >= 0; i = i -1 ) {
    let multiply = array[i] * 2;// double it
    if (multiply > 9) {//check if that number is bigger than 9
      let minusNine = multiply -9;//subtract nine from it
            res.push(minusNine);
    }else{
      res.push(array[i]); //just store as it is.
    }
  }
  return res.reverse()
};
validateCred([10,3,40,9]);// [11, 3, 71, 9]
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