Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialnicholas maddren
12,793 PointsI can't do this on a calculator never mind in code :(
This is the challenge:
Create a variable named "nineteen" and assign the correct numeric value 19 to it using only variables a, b, and c and the common numeric operators.
The only numbers are a = 1, b = 2, c = 4.
I can't get the number 19 on a calculator never mind the code.
Any idea how I can do this?
3 Answers
Stone Preston
42,016 Pointsvar nineteen = (b * c) + (b * b) + c + b + a;
would work. there are lots of solutions.
var nineteen = (c * c) + b + a
would work as well.
what I did was just use numbers first (but only use the numbers that a, b and c store. so first I wrote out
(2 * 4) + (2 * 2) which is 12, so we have 7 left
then just added that 7 on using 4, 2 and 1
(2 * 4) + (2 * 2) + 4 + 2 + 1
then I just replaced the numbers with the matching variables
var nineteen = (b * c) + (b * b) + c + b + a;
James Barnett
39,199 PointsHere's how to break the problem down into 5 steps:
It's basically long division combined with find / replace.
- What's the largest block we have to work with? it's
4
- How many times does
4
go into19
?4 * 4 = 16
with3
left over - Since
c = 4
, we would translate that as(c * c) = 16
- We have
3
leftover, sinceb = 2
&a = 1
, so we can just add them together,b + a = 3
- Put what we got from steps 2 & 4 together, make sure to remember order of operations and and use the parenthesis, so we get
(c * c) + b + a = 19
var nineteen = (c * c) + b + a;
n1ckbren
3,376 Points:D correct answer var nineteen = a + a + a + a + a + a + a + a + a + a + a + a + a + a + a + a + a + a + a
James Barnett
39,199 PointsThat's perfectly valid.
TIMTOWTDI - There's more than one way to do it.