问题
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?
回答1:
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 ofa + 1
toa
, then returns the value ofa
.a++
is called post-increment. It first returns the value ofa
, then assignsa + 1
toa
.
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
回答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.
回答3:
alice = alice + 1
works like this:
- take
alice
value (0) into temp variable or CPU register - add 1 to temp
- store temp in
alice
(1)
alice = alice++
works like this:
- take alice value (0) into temp variable or CPU register
- increment
alice
by 1 and store it inalice
(i.e.alice++
) - 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".
回答4:
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
回答5:
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!
回答6:
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
回答7:
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;
回答8:
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.
回答9:
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.
来源:https://stackoverflow.com/questions/42459444/difference-between-i-i-and-i-i-1