Basic JavaScript - Compound Assignment With Augmented Subtraction

Tell us what’s happening:
Attempting to code so that a will equal 5, b will equal -6, and c will equal 2.

Your code so far

let a = 11;
let b = 9;
let c = 3;

// Only change code below this line
let targetA = 5
let targetB = -6
let targetC = 2

let subToA = targetA - a;
let subToB = targetB - b;
let subToC = targetC - c;

a -= subToA;
b -= subToB;
c -= subToC;

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36 OPR/102.0.0.0

Challenge: Basic JavaScript - Compound Assignment With Augmented Subtraction

Link to the challenge:

let subToA = targetA - a;

5 - 11 = -6

a -= subToA

a = a - (-6)
a = 11 + 6
a = 17

It should help if you print your results to console so you can see it:

console.log(a,b,c);

let a = 11;

let b = 9;

let c = 3;

// Only change code below this line

let targetA = 5;

let targetB = -6;

let targetC = 2;

a -= a - targetA;

b -= b - targetB;

c -= c - targetC;

console.log(a, b, c);

// running tests
// tests completed
// console output
5 -6 2

Thanks for the help!

I think you have misunderstood the Subtraction assignment.


You do not have to change the numbers or create new variables.

All you do is remove the variable from the right-hand side and move the operator to before the left-hand side assignment.

let life = 44;
life = life - 2;
console.log(life) // 42
let life = 44;
life -= 2;
console.log(life) // 42

I was just trying to find a way to solve without having to math for the missing variables

The math is already done for you.

Please change this for me to log 10 using the subtraction assignment. Post your code.

let number = 12;
number = number - 2;
console.log(number);
a -= a - targetA;
11 = 11 - (11 - 5)

It’s quite a funny solution, since you know you want a to equal 5. The most obvious solution is

a = 5

but if you must use -= operator to set a to an arbitrary number, it seems elegant but looks jacked.

There is no ‘setting a (or b or c) to an “arbitrary number”’. This challenge is about replacing the 3 lines of code with a new 3 lines that mean the exact same thing but are written to be a little bit shorter.

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