Why Can’t Conditional Operator Be Used To Conditionally Increment Property Like This?
Why do I receive a linter warning when using the conditional operator, but using a simple if statement does not? Incorrect switch (type) { case 'add': array[index]['Quantity'
Solution 1:
I think this is the problem:
array[index]['Quantity'] > 0 ? array[index]['Quantity']-=1 : 0; // Error
break;
You are returning an expression (array[index]['Quantity']-=1) when the ternary condition is true, when you should be returning a value.
Try this:
array[index]['Quantity'] = array[index]['Quantity'] > 0 ? array[index]['Quantity'] - 1 : 0;
break;
But I still think this is the best solution:
if (array[index]['Quantity'] > 0)
array[index]['Quantity']--;
break;
Post a Comment for "Why Can’t Conditional Operator Be Used To Conditionally Increment Property Like This?"