0

https://www.hackerrank.com/challenges/compare-the-triplets

var alice = bob = 0;
if(a0 > b0){
  alice = alice + 1;
} else if (a0 < b0) {
  bob = bob + 1;
} else if (a1 > b1) {
  alice = alice + 1;
} else if (a1 < b1) {
  bob = bob + 1;
} else if (a2 > b2) {
  alice = alice + 1;
} else if (a2 < b2) {
  bob = bob + 1;
}

console.log(alice, bob);

VS

var alice = bob = 0;
if(a0 > b0){
  alice = alice++;
  console.log(alice);
} else if (a0 < b0) {
  bob = bob++;
} else if (a1 > b1) {
  alice = alice++;
} else if (a1 < b1) {
  bob = bob++;
} else if (a2 > b2) {
  alice = alice++;
} else if (a2 < b2) {
  bob = bob++;
}

console.log(alice, bob);

first one worked properly but the second one didn't. Can someone help me what are the differences between the two?

user2864740
  • 60,010
  • 15
  • 145
  • 220
  • 1
    Don't do `alice = alice++;` -- that is really confusing, and not intended use. You should do just `alice++;`. – trincot Feb 25 '17 at 17:46
  • Does this answer your question? [Postfix and prefix increments in JavaScript](https://stackoverflow.com/questions/49550526/postfix-and-prefix-increments-in-javascript) – Sebastian Simon Mar 09 '21 at 13:08

9 Answers9

4

That is because the first one assigns a new value to the variable, while the second one returns and increments it.

a = a + 1

This is a simple variable assignment, where the value of a + 1 is assigned to a. After this line executes, a will be incremented by one.

a++ / ++a

This is the increment operator. It not only increments the variable by 1, but also returns its value.

  • ++a is called pre-increment. It assigns the value of a + 1 to a, then returns the value of a.
  • a++ is called post-increment. It first returns the value of a, then assigns a + 1 to a.

This is the cause for the error you're seeing. It means that in your initial example, this is what happens in your code:

alice = alice++; // assings the original value of alice to alice, then increments it
  console.log(alice); // still the initial value

By pre-incrementing the variable, you would get:

alice = ++alice; // assings alice+1 to alice
  console.log(alice); // now contains the value of alice+1
ppajer
  • 3,045
  • 1
  • 15
  • 22
2

Javascript increment operator ++ is used with the variable all alone to increment it. So alice = alice + 1; and alice++ are equivalent. If you use alice = alice++;, it will just keep the original value of the variable.

Ken H.
  • 408
  • 1
  • 3
  • 11
2

alice = alice + 1 works like this:

  1. take alice value (0) into temp variable or CPU register
  2. add 1 to temp
  3. store temp in alice (1)

alice = alice++ works like this:

  1. take alice value (0) into temp variable or CPU register
  2. increment alice by 1 and store it in alice (i.e. alice++)
  3. store temp variable in alice (i.e. 0 goes back)

In all languages where postfix and prefix ++ is present, postfix notation means "use the value first, then increment" while prefix means "increment, then use the value".

Alex Pakka
  • 9,466
  • 3
  • 45
  • 69
1
alice++ is postfix increment of alice.That means that the value of alice will be first used and then incremented. So, in the second code the value is first assigned to the left variable and then incremented.

If you will change the second part of the code to ++alice(similarly other variables also) i.e. the prefix increment it will give correct results. Prefix operator first increments the value and then uses the value. So it will increment and then assign the value to the left variable.

Read here

Ayush
  • 741
  • 9
  • 19
1

Don't do alice = alice++; -- that is really confusing, and not intended use. You should do just alice++;

This is what happens with the first statement:

alice++ increments the value in the variable alice, but returns the original value.

Then this returned value is assigned the the variable at the left of the assignment operator: alice = ..., and so you assign back the original value, losing the increment that was briefly made!

trincot
  • 317,000
  • 35
  • 244
  • 286
1

alice = alice++ is a postfix expression. Here the value gets assigned to alice first and then it its incremented. Look at the below example to understand the difference

var b = 1
a = b++            // sets a as 1 and  then increase the value of b
console.log(a,b)   //Prints 1,2
a = ++a            // Increases value of a by 1 and then sets a
console.log(a)     //Prints 2
sanket
  • 664
  • 6
  • 16
0

When you use the JavaScript "unary" operators (operators that take one operand: i.e. ++ and --), you can place them at the beginning or the end of an expression. Where you put them determines when they do their job. If it is at the beginning, the operation is done first and then the rest of the expression is carried out and if it is at the end, the expression is carried out and the the operation takes place. These are known as pre and post increment and pre and post decrements.

So:

alice = alice++; 

Essentially means alice = alice and then bump up the value of alice by 1.

Whereas:

alice = ++alice;

Means to bump up the value of alice by one and assign the result back to alice. That is the same as writing:

alice = alice + 1;
Scott Marcus
  • 64,069
  • 6
  • 49
  • 71
  • "Essentially means alice = alice and then bump up the value of alice by 1." is incorrect. The post-increment assignment is applied immediately, not at the end of the entire expression. – user2864740 Feb 25 '17 at 19:01
  • @user2864740 MDN States: *"Increment (++) The increment operator increments (adds one to) its operand and returns a value. If used postfix, with operator after operand (for example, x++), then it returns the value before incrementing. If used prefix with operator before operand (for example, ++x), then it returns the value after incrementing."* My explanation is correct. https://jsfiddle.net/54cscj06/3/ – Scott Marcus Feb 25 '17 at 19:35
0

What you're doing wrong is here:

variable = variable++

The ++ operator adds 1 to an integer and then saves the integer. This means that you don't have to write a variable = there.

For instance, in the following code:

var myVar = 0;
myVar++;

myVar's value is now 1. There is no need to separately declare it. And I don't think the ++ method returns the value (correct me if I'm wrong, though).

So you need to use this code in the second case:

var alice = bob = 0;
if(a0 > b0){
  alice++;
  console.log(alice);
} else if (a0 < b0) {
  bob++;
} else if (a1 > b1) {
  alice++;
} else if (a1 < b1) {
  bob++;
} else if (a2 > b2) {
  alice++;
} else if (a2 < b2) {
  bob++;
}

console.log(alice, bob);

Hope it helps.

frick
  • 166
  • 9
  • *"The `++` operator adds 1 to an integer and then saves the integer. "* Well, that's only part of the story. It's not so much that it adds one to the value, but it's more about the fact that it does it **after** the rest of the expression is complete. – Scott Marcus Feb 25 '17 at 17:53
  • @ScottMarcus That is incorrect: `x++` increments `x` as soon as *that* expression (`x++`) is evaluated. The issue is that the pre-increment value of `x` is returned by the expression and then *that* value is assigned, making the entire operation a No-OP. – user2864740 Feb 25 '17 at 18:40
0

In JavaScript, the expression x = x++ will not change the value of x (if x is a number1).

This is because the post-increment arithmetic operator evaluates to current value and then immediately updates the target (eg. x) with the incremented value.

However, since the result of evaluating x++ is the original value, and not the incremented value now stored in x, the end result is that x has the original value re-assigned to it.

That is, x = x++ is logically equivalent to:

// given x = 1, then
f = x++;
// f = 1
// x = 2
x = f;
// x = 1

Useful forms for statements that increment a variable by one include: x++ (the assignment is implicit), x += 1, and x = x + 1. The difference is that x++ evaluates evaluates to the original value while the other two forms evaluate to the new value.


1 The post-increment operator always yields number, converting as necessary. Thus it can change the value of x in certain conditions: eg. after x = "foo"; x = x++;, x will be NaN.

user2864740
  • 60,010
  • 15
  • 145
  • 220