Challenge Waypoint: Decrement a Number with Javascript has an issue.
User Agent is: Mozilla/5.0 (Windows NT 10.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36
.
Please describe how to reproduce this issue, and include links to screenshots if possible.
My code:
var myVar = 11;
// Only change code below this line
myVar = myVar--;
This code returns 11
, but in test is number 10
, for which works myVar = --myVar;
, but that does not pass "Use the -- operator on myVar".
:+1:
This test expects to post fix decrement operator , however having myVar = myVar
did confused me too. Either keep it and include feature for prefix decrement operator or remove the line myVar = myVar
after comment all together.
The solution is to remove the second line myVar = myVar
and just put myVar--
.
While assigning with postfix decrement , the decrement value is not assigned instead the original value is assigned. However when assigning with prefix decrement operator the decrement operator is applied.
This given example myVar = --myVar;
is not so obvious, but for example someVar = --myVar;
is common practise and this lesson probably should teach and allow both ways.
I agree these two tests should teach both postfix decrement/increment operators and prefix decrement/increment operators or at least allow use of both . Although, I am not sure what is the importance of variables names here. Having different or having same , they both makes sense but keeping myVar = myVar
when solution is just myVar--
or myVar++
doesn't makes sense to me.
prefix decrement is very uncommon in javascript and this waypoint is specifically teaching postfix. Postfix will pass as long as you're not doing assignment since postfix negates the need for assignment.
Thanks and happy coding!
This is why this comment was added to the description:
@SaintPeter thank you for the clarification , however @ltegman it doesn't hold any water whether what is commonly used in JavaScript in choosing between prefix and postfix increment/decrement operators, they are included in the language for a reason. Both postfix and prefix increment/decrement operators doesn't need any kind of assignment.
removed solution by mod
Most helpful comment
This given example
myVar = --myVar;
is not so obvious, but for examplesomeVar = --myVar;
is common practise and this lesson probably should teach and allow both ways.