[SOLVED] Compound Assignment With Augmented Addition (Help needed)

[SOLVED] Compound Assignment With Augmented Addition (Help needed)
0

#1

Tell us what’s happening:
I’m so stuck on this, I really don’t know what to do

Your code so far

var a = 3;
var b = 17;
var c = 12;

// Only modify code below this line

a = a + 12;
b = 9 + b;
c = c + 7;


**Link to the challenge:**
https://www.freecodecamp.org/challenges/compound-assignment-with-augmented-addition

#2

You’re gonna use the compound assignment operator += to add a number to a variable and assign it at the same time.

If you have

a = a + 42;

you can use += as a shorter version:

a += 42;

#3

Thank You so much, do you also know how to do the subtraction bit? I’m confused
var a = 11;
var b = 9;
var c = 3;

// Only modify code below this line

a -= a - 6;
b -= b - 15;
c -= c - 1;
that’s what I have so far


#4

It’s also the same idea/pattern with subtraction (and in general, operations that require two operands).


#5

Is it the same for Multiplication and Division or is it another concept?


#6

Yep. Totally the same :wink:


#7

I’m really confused :frowning:


#8

Can you tell what exactly confuses you?


#9

This is what I came up with, for the multiplication

var a = 5;
var b = 12;
var c = 4.6;

// Only modify code below this line

a *= 25;
b *= 36;
c *= 46;


#10

What am I doing wrong?


#11

Why are you multiplying a by 25 and assigning it back to a? The original code multiplied a by 5 and assigned it back to a.


#12

I’m very confused, I thought what I was doing was making it right


#13

I will just use the code involving variable a and then you can apply the logic to the other variables (b and c).

Original code for variable a is below:

var a = 5;
a = a * 5;

You are just going to replace the 2nd line with code that uses the *= operator. So you will have:

a *= ???;

You just have figure out what ??? needs to be in the above line. Remember, the original code multiplied a by 5 and then assigned it back to a.


#14

Thanks, I got how to do it. I really appreciate your help


#15

The pattern of operating on a variable and assigning it back to that variable is so common that there’s a short-hand way of writing it.

Instead of writing a = a + 3 you can just write a += 3. Same for all the other arithmetic operators. It’s just a different way of writing the same thing, but you only need to refer to the named variable once.


#16

Thanks, I got the hang of it now :smiley: