Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Increment variable by more than 1?

Here's my script :

function itemQuantityHandler(operation, cart_item) {
  var v = cart_item.quantity;

  //add one
  if (operation === 'add' && v < settings.productBuyLimit) {
    v++;
  }

  //substract one
  if (operation === 'subtract' && v > 1) {
    v--;
  }

  //update quantity in shopping cart
  $('.item-quantity').text(v);

  //save new quantity to cart
  cart_item.quantity = v;
}

What I need is to increase v (cart_item.quantity) by more than one. Here, it's using v++, but it's only increasing by 1. How can I change this to make it increase by 4 every time I click on the plus icon?

I tried

v++ +4

But it's not working.

like image 440
larin555 Avatar asked May 17 '12 20:05

larin555


People also ask

Can I increment 2 in for loop?

A for loop doesn't increment anything. Your code used in the for statement does.

How do you increment the value of a variable by 1?

A program can increment by 1 the value of a variable called c using the increment operator, ++, rather than the expression c=c+1 or c+=1. An increment or decrement operator that is prefixed to (placed before) a variable is referred to as the prefix increment or prefix decrement operator, respectively.

How do you increment variables?

The most simple way to increment/decrement a variable is by using the + and - operators. This method allows you increment/decrement the variable by any value you want.

How do you increment a variable by a number?

Adding 1 to a variable is called incrementing and subtracting 1 from a variable is called decrementing. increment and decrement operators work only with integer variables -- not on floating point variables or literals. the C++ compiler is controlling the execution of the prefix and postfix operators.


3 Answers

Use a compound assignment operator:

v += 4;
like image 145
helpermethod Avatar answered Oct 18 '22 03:10

helpermethod


Use variable += value; to increment by more than one:

v += 4;

It works with some other operators too:

v -= 4;
v *= 4;
v /= 4;
v %= 4;
v <<= 1;
v >>= 4;
like image 20
We Are All Monica Avatar answered Oct 18 '22 04:10

We Are All Monica


To increase v by n: v += n

like image 24
Kvam Avatar answered Oct 18 '22 03:10

Kvam