3

I am writing next_previous() function for pagination purpose, I have for loop which is moving from 0 to the given length. I want to use the same loop for two cases from 0 to 10 and from 10 to 0.

for (var i = 0; i < 10; i++) {
}

for (var i = 10; i > 0; i--) {
}

to use both cases in one loop I am doing something like this but not working

var  i = 0; a = '', b = '';

if(somevar === true){
   i = 0 , a = '++', var b = '<';
}else{
   i = 10 , a = '--', var b = '>';
}

for (i; i +b+ 0; i+a) {
} 

now problem is javascript not allowing concatenation this way, how can I achieve this?

See screenshot

3
  • What are you trying to achieve? Can you post a sample of the output you want? Commented Nov 22, 2017 at 7:30
  • I have update question with screenshot @Eddie Commented Nov 22, 2017 at 7:32
  • I don't know any language that allows "concatenation" like this. It looks like you expect + to behave like eval. Commented Nov 22, 2017 at 7:38

4 Answers 4

3

Try this approach which uses for the logic part ( increment and condition ) functions.

ES6

let i = 0;
let a;
let b;
let count = 0;

let somevar = true;

if(somevar) {
   i = 0;
   count = 10;
   a = () => i++;
   b = () => i < count;
} else {
   i = 10;
   count = 0;
   a = () => i--;
   b = () => i > count;
}

for (; b(); a()) {
   console.log(i);
}

ES5

var i = 0;
var a;
var b;
var count = 0;

var somevar = true;

if(somevar) {
   i = 0;
   count = 10;
   a = function() { i++; };
   b = function() { return i < count; };
} else {
   i = 10;
   count = 0;
   a = function() { i--; };
   b = function() { return i > count; };
}

for (; b(); a()) {
   console.log(i);
}

Sign up to request clarification or add additional context in comments.

6 Comments

Suren can you please do this in javascript.
This is Javascript :)
I think it's ECMA SCRIPT or something
This is just ES6 specification
one little question Suren?
|
3

It seems like you are looking for an eval solution, but this really is not how one would approach this problem. Rather go for a functional design:

function forward(cb) {
    for (var i = 0; i < 10; i++) cb(i);
}
function backward(cb) {
    for (var i = 10; i > 0; i--) cb(i);
}

const loop = somevar ? forward : backward;
loop(i => {
    …
});

Comments

0

This answer is a less-lazy version of @BiswajitPanday's answer. I would have edited his answer instead, but I realized that it's been a week since this question was posted, so an edit on his answer wouldn't be as fruitful since OP wouldn't be notified



Here is an eval solution:

function loopFoo(backwardsDirection, callback) {
  if(backwardsDirection === true) {
  var i = 10, b = '>', c = 0, a = '--';
  } else {
  var i = 0, b = '<', c = 10, a = '++';	
  }

  for (i; eval("i" + b + c); eval("i" + a)) {
  callback(i);	
  }
}


console.log("Forwards:");
loopFoo(false, console.log);

console.log("Backwards:");
loopFoo(true, console.log);

console.log("Forwards:");
loopFoo(false, console.log);

That function (eval) is generally frowned upon though, I would go with @Bergi's solution, if not that, @Suren's, and if not his either, my solution.

Comments

-1

Just use eval(i+b+10) instead of i+b+10

3 Comments

But i is 0 not "i".
Some int + some string produces a string. As b is string so i will be also a string after concatenate.
Yes, but it produces the wrong string to be evaluated, at least in the case eval(i+a).

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.