3

Depending on a certain condition, I want one of two different for loops to execute. So in the example below, I want console.log to return descending numbers if a > b, and ascending numbers if b > a. I was in no way surprised that this didn't work, but it gives you the idea of what I'm after.

for (if (a > b) {(i = 2; i > 0; i--)} else {(i = 0; i < 2; i++)} {
console.log(i);
};

Obviously I could work around this by having if-else select between two complete and separate loops each with their own console.log(i) code block, but in the case I'm dealing with, that will involve duplicating a huge amount of code.

2
  • 2
    what code would be duplicated a bunch? The body of the loop? If so, extract it to a separate function. Commented Oct 8, 2018 at 16:14
  • 1
    what you want to achieve? post some sample input and expected output. I think you're doing over engineering with simple task i.e getting ascending and descending numbers with if and for Commented Oct 8, 2018 at 16:15

5 Answers 5

4
 for(let i = a > b ? 2 : 0 ; a > b ? i > 0 : i < 2; a > b ? i-- : i++)
   console.log(i);

you have to move the condition into each expression. Or move the body in its own function:

 function body(i) {
   console.log(i);
 }

 if (a > b) {
   for(i = 2; i > 0; i--) body(i);
 } else {
   for(i = 0; i < 2; i++) body(i);
}

Or the functional way:

 const maybe = (condition, op) => arg => condition ? op(arg) : arg;
 const range = (start, end) => Array.from({ length: end - start }, (_, i) => start + i);

maybe(a > b, it => it.reverse())(range(0, 2)).forEach(console.log);
Sign up to request clarification or add additional context in comments.

Comments

1

You could put the body of the loop into a function, and on every iteration of both loops, call that function. Also note that for loop } end brackets should not have semicolons at the end:

const a = 4;
const b = 5;

if (a > b) {
  for (let i = 2; i > 0; i--) {
    loop(i);
  }
} else {
  for (i = 0; i < 2; i++) {
    loop(i);
  }
}
function loop(i) {
  console.log(i);
}

Comments

1

jsfiddle

function choose(a, b) {
  if (a > b) {
    for (let i = 2; i >= 0; i--) {
      console.log(i);
    }
  } else {
    for (let i = 0; i <= 2; i++) {
      console.log(i);
    }
  }
}

choose(2, 4);

Comments

0

I would turn it around like this: First do if statement then for loop.

if(a > b){
  for(let i = 2; i > 0; i--) console.log(i)
} else {
  for(let i = 0; i < 2; i++) console.log(i)
}

Comments

0

Just try wrapping it in a function like this:

function loop(a, b) {
  if (a > b) {
    for (i = 2; i > 0; i--) {
      console.log(i);
    }
  } else {
    for (i = 0; i < 2; i++) {
      console.log(i);
    }
  }

}


loop(5,4);
loop(4,5);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.