0

Is there a way to change the value of several variables in one line? The code below is working, but very repetitive. How can I change the values of i, j, k in a more elegant way? Also is it possible to write an if function for all three variables instead of repeating it three times.

var i = 0;
var j = -1;
var k = -2;

function fn() {
   // magic happening;

    i += 1;
    j += 1;
    k += 1;

    if (i > 3) {
        i = 0;
    }
    if (j > 3) {
        j = 0;
    }
    if (k > 3) {
        k = 0;
    }

    // more stuff happening
}
3
  • Use a comma? i += 1, j += 1, k += 1; - Also, you could use the ternary operator to make it shorter i = (i > 3) 0 : ++i. Commented Sep 6, 2016 at 8:47
  • using commas i +=1, j +=1, ... ; Commented Sep 6, 2016 at 8:47
  • try this var i = 0, j = -1, k = -2; and i += 1, j+=1, k+=1 Commented Sep 6, 2016 at 8:48

6 Answers 6

1

Try this:

var i = 0;
var j = -1;
var k = -2;

function fn() {
    i = (i <= 2) ? ++i : 0;
    j = (j <= 2) ? ++j : 0;
    k = (k <= 2) ? ++k : 0;
}

Or by writing another function:

var i = 0;
var j = -1;
var k = -2;

function fn() {
    i = calculateIt(i);
    j = calculateIt(j);
    k = calculateIt(k);
}

function calculateIt(value) {
    return (value <= 2) ? ++value : 0;
}
Sign up to request clarification or add additional context in comments.

1 Comment

[(i+1)>3] should become [i > 2]
0

You could use a single expression for one assignment.

i = i > 2 ? 0 : i + 1;

You can make an function which keeps the interval.

function incAndLimit(v) {
    return v > 2 ? 0 : v + 1;
}

// usage
i = incAndLimit(i);

Comments

0

Assign value like this:

i++,j++,k++;

1 Comment

This ignores the event where i,j,k are higher than 3.
0
var [i, j, k] = [0, -1, -2]
                .map(v => v + 1)
                .map(v => v > 3 ? 0 : v);

Note that this code requires ES6 compatible environment.

2 Comments

This only works for declaring the variables, it doesn't work for changing their values once they've been created.
You can always redeclare them with var. But yea, there might be a cleaner way depending on what you want to do inside the code. The functional way would be to chain all actions together so you need to make only one declaration.
0

If you can put the variables in an array you can use ES2015's .map and lambdas:

var list = [0, -1, -2] // replaces separate i, j, and k
list = list.map(x => x++)
           .map(x => x > 3 ? 0 : x);

You can combine these two functions

list = list.map(x => {
    x++;
    return x > 3 ? 0 : x
});

Even if you can't put these variables in an array I'd suggest putting the checking function in a separate function:

function abv3(x) {
    x++;
    return x > 3 ? 0 : x;
}

i = abv3(i), j = abv3(j), k = abv3(k);

Comments

0

I guess this is it by double ES6 array destructuring

var  i = 0,
     j = -1,
     k = -2,
update = ([x,y,z]) => [++x>3?0:x, ++y>3?0:y, ++z>3?0:z];

[i,j,k] = update([i,j,k]);
console.log(i,j,k);
[i,j,k] = update([i,j,k]);
console.log(i,j,k);
[i,j,k] = update([i,j,k]);
console.log(i,j,k);
[i,j,k] = update([i,j,k]);
console.log(i,j,k);
[i,j,k] = update([i,j,k]);
console.log(i,j,k);
[i,j,k] = update([i,j,k]);
console.log(i,j,k);
[i,j,k] = update([i,j,k]);
console.log(i,j,k);
[i,j,k] = update([i,j,k]);
console.log(i,j,k);
[i,j,k] = update([i,j,k]);
console.log(i,j,k);
[i,j,k] = update([i,j,k]);
console.log(i,j,k);
[i,j,k] = update([i,j,k]);
console.log(i,j,k);
[i,j,k] = update([i,j,k]);
console.log(i,j,k);
[i,j,k] = update([i,j,k]);
console.log(i,j,k);

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.