VARIABLES: Mathematical Assignment Operators___Wrong Answer Output?


#1

Hello – I’m new and want to be sure I’m learning this correctly:

<Below this line, add a link to the EXACT exercise that you are stuck at.>
https://www.codecademy.com/courses/learn-javascript-introduction/lessons/variables/exercises/mathematical-shortcuts?action=lesson_resume&course_redirect=introduction-to-javascript

<Below this line, in what way does your code behave incorrectly? Include ALL error messages.>

Question #3 is asking to use the increment operator to increase the value saved to the assay by 1,
but I keep getting the same output for the saved variable; let assay = 3
From my screen shot, please note I’m writing:

console.log(assay++);

Why isn’t the output reading as 4?

Thanks!

```

let molecule = 16;
let particle = 18;
let assay = 3;

// Add and assign to molecule below
console.log(molecule += 16);

// Multiply and assign to particle below
console.log(particle *= 6.02);

// Increment assay by 1
console.log(assay++);

<do not remove the three backticks above>

#2

variable++ is a postfix operation. I.e. you are printing the value 3 then incrementing it to 4.
You could use a prefix i.e. ++variable, which would increment it first, then print.
You could do a += 1 instead
Or variable = variable + 1

They all work.
The important take away is understanding post and pre fix


#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.