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 trial
maryclairegilmore
Courses Plus Student 30 PointsMy code returns the correct solution, but I'm not passing the toy problem test
var x = 7;
var y = 8;
var z = 9;
function answer() {
x = y + z;
z = x;
x = y;
z = x + y;
y = z - x;
z = y;
x = y * z;
z = y / z;
x = (z - x) / (y + z);
}
function finalValueOfX(x) {
if(x === 7) {
return x;
} else {
return undefined;
}
}
finalValueOfX(x);
//variable juggling with scalars should evaluate value of x correctly
//Expected false to be true.
//in <spec> - line 4
//describe('variable juggling with scalars', function() {
// it('should evaluate value of x correctly', function() {
// Note: intentionally hiding the expected value.
// expect(answer.finalValueOfX === -7).toBe(true); // < line 4
// });
//});
Moderator edited: Added markdown so that the code renders properly on the forums.
2 Answers
Jennifer Nordell
Treehouse TeacherWhile I'm not sure to which challenge you're referring, I can tell you this much so far. According to the code you've posted, the value of x starts at 7 and never changes. So the value returned will be 7.
At no point do you ever call the answer() function which would alter the value of x. Also, in your comments it says that answer.finalValueOfX === -7 should be true. The syntax of this suggests that finalValueOfX is a property on an object named answer. But your code contains no custom made object named answer. According to the comments, you should be checking against an explicit equality of -7 and not positive 7.
This will be infinitely easier to help you with if we can see the problem you're attempting to solve!
Note: After running the answer() function, the value of x will be negative 7 (-7). Without running the answer function, the value of x remains at positive 7 (7).
maryclairegilmore
Courses Plus Student 30 PointsJennifer Nordell
Treehouse TeacherTo reiterate, you never call the answer() function in the code you posted above. This means that the value of x will be unchanged and remain at 7. But if you were to run the answer() function, the final value of x would be negative seven (-7).
You have included this comment:
// expect(answer.finalValueOfX === -7).toBe(true);
But then you have this code:
function finalValueOfX(x) {
if(x === 7) { // You are checking for positive 7 here and not -7 as indicated in the quote above
return x;
} else {
return undefined;
}
}
The gist you posted does include your code, but I'm still curious as to where you are getting this assignment from. I'd like to see the full instructions, if possible.
Jennifer Nordell
Treehouse TeacherJennifer Nordell
Treehouse TeacherHi there! For some reason, I don't recognize this challenge. Could you post a link, please?