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

101
Vistas
Counting up and then back down in a for loop javascript
Function oddNumbers(){
Var odds ="";
For(index=0; index <=10; index ++){
If(index%2 !=0){odds+=index +"."}
}
}

OddNumbers();

Trying to count all odd counting numbers up then back down again in a for loop. I can get it to go up with the code again. But when I try nested for I cannot get it to go back down again in the same loop. How would I get it to loop up then back down?

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Assuming you want to use only one loop definition, you have to define custom conditionExpression and incrementExpression.

Here is a quick example.

var direction = 1;

//REM: What gets added on each iteration?
function incrementExpression(){
  //REM: Increase or decrease depending on direction
    return 1*direction
}

//REM: What gets evaluated on each iteration?
function conditionExpression(i){
    if(i < 10 && direction === 1){
        return true
    }
    if(i === 10 && direction === 1){
        direction = -1
        return true
    }
    if(i >= 0 && direction === -1){
        direction = -1
        return true
    };

    //REM: Returns false if direction downwards and i lower zero
    return false
}

for(var i=0; conditionExpression(i); i+=incrementExpression(i)){
    console.log(i)
}

about 4 years ago · Juan Pablo Isaza Denunciar

0

You can reverse your for loop on your use-case. See code snippet below:

function oddNumbers() {
var odds = "";
  
  for(index=0; index <=10; index ++){
    if(index%2 !=0) {
      odds+=index +"."
      console.log(odds);
    }
  }
  
  for(index = 10; index > 0; index--) {
    if(index % 2 != 0) {
    odds+=index +"."
        console.log(odds);
    }
  }
}

oddNumbers();

Im not sure if that's what you want but that's what I understand on your given code on your question.

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