JavaScript Reducing Multiple Variables in JavaScript reduce multiple values,reduce multiple values down to a single

Can I reduce multiple variables in JavaScript? For example if I have 3 variables:

     var foo = 10;
     var boo = 15;
     var lol = 15;

     //Normal syntax:
     foo -=1; // -> foo will be 9
     boo -=1; // -> boo will be 14
     lol -=1; // -> lol will be 14

Is there an option to do it with one line? or better syntax? For example: foo ,boo ,lol -=1;

Answer:1

No, you cannot. Standard JavaScript does not give this opportunity. The worse thing that you nearly cannot do it functionally as well. lol -=1; or lol--; is just a shortcut for lol = lol - 1;. So if you will try to write a function, which does that for you e.g.:

function reduce() {
  for (var i = 0; i < arguments.length; i++) {
    arguments[i] = arguments[i] - 1;
  }
}

and then call it like

reduce(foo, bar, lol);

it just won't work because you pass primitive numbers (not references). Every time you change the number inside of the function it won't change the number itself but it will return a new number instead.

This could be solved by using some object to store all the variables, e.g.:

var vars = {
  foo: 12,
  bar: 13,
  lol: 14
};

function reduce(variables) {
  for (variable in variables) {
    if (variables.hasOwnProperty(variable)) {
      variables[variable] -= 1;
    }
  }

  return variables;
}

reduce(vars);

But this is not a list of 3 variables, this is kind of a context you attach them to.

If you do the stuff in a global scope (e.g. in a window without wrapping the stuff in a function), you can combine both ways above into one (Window stores all var-declared variables inside):

function reduce(vars) {
  var varslist = vars.split(',');

  for (var i = 0; i < varslist.length; i++) {
    window[varslist[i]] -= 1;
  }
}

reduce('foo,boo,lol');

but as soon as you move it to some subcontext it won't work any longer. Also it looks very nasty. I would rather prefer the second solution with vars object representing your variables context.

Answer:2

Even though console log give the while expression as false the loop runs infinetly. var tile_height; $(document).ready(function(){ tile_height = $("#department > .front").height(); }); ...

Even though console log give the while expression as false the loop runs infinetly. var tile_height; $(document).ready(function(){ tile_height = $("#department > .front").height(); }); ...

Short question. How does one find the dimensions of a mesh in three.js? I have Collada (.dae) files which I would like to know the size of in units (x,y,z). I have seen comments about using geometry....

Short question. How does one find the dimensions of a mesh in three.js? I have Collada (.dae) files which I would like to know the size of in units (x,y,z). I have seen comments about using geometry....

Within the code below contains a very simple question game (work-in-progress). So far, I've been able to have a "Rules" message appear through the onLoad function, "qDisplayLoad()". Next, the onClick ...

Within the code below contains a very simple question game (work-in-progress). So far, I've been able to have a "Rules" message appear through the onLoad function, "qDisplayLoad()". Next, the onClick ...

  1. splice object from array
  2. splice object from array typescript
  3. splice object from array angular 2
  4. splice object from array js
  5. splice object from array angularjs
  6. splice object from array jquery
  7. splice from object
  8. splice object in javascript
  9. splice object in angularjs
  10. splice object in c++
  11. splice specific object from array
  12. splice remove object from array
  13. splice json object from array
  14. object splice in js
  15. splice not removing object from array
  16. javascript splice remove object from array

So basically when i hover over "drive" option which is a h1, i want a div container to appear that contains several other in detail options. Then if i click on "ride" option which is another h1, i ...

So basically when i hover over "drive" option which is a h1, i want a div container to appear that contains several other in detail options. Then if i click on "ride" option which is another h1, i ...

  1. change style element javascript
  2. change style element jquery
  3. change style element js
  4. change style element
  5. change style element angular
  6. change style element angularjs
  7. change element style css
  8. change element style react
  9. change element style typescript
  10. change element style using javascript
  11. change element style angular 4
  12. change element.style wordpress
  13. change element style in iframe
  14. change element style on click
  15. change element style class javascript
  16. change element style inside iframe
  17. change element style html
  18. change element style using jquery
  19. change element style color
  20. change style dom element