0

Why does this code:

var score = 0;
score = ("H".charCodeAt(0) === "H".charCodeAt(0)) ? score += 1 : score;
console.log("score should be one here and it is = " + score);

while this below does not seem to work:

var score = 0;
score = ("H".charCodeAt(0) === "H".charCodeAt(0)) ? score ++ : score;
console.log("score should be one here but its (zero) = " + score);

Looks technically the same to me? Am using nodejs to run this.

Marka A
  • 258
  • 5
  • 15
  • 1
    Neither code looks good. Assign to score *only once* for clear code - don't assign inside the conditional *and also* as the result of the conditional. `score = (condition) ? score + 1 : score` or even `if (condition) score++` – CertainPerformance Nov 25 '18 at 12:38
  • Thanks @CertainPerformance. Apologies for missing that earlier answer. – Marka A Nov 25 '18 at 12:51

0 Answers0