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

JavaScript

Function, Scope Challenge

Help, please. I'm really struggling with JS cause I just don't understand coding like this. HTML and CSS were wonderful but JS has fustrated on almost every code challenge. It doesn't help when they are way more complex than what the video just showed. Right now I'm stuck in the Functions chapter on the Scope challenge and I can't see what to do.

When the 'hypotenuse' function is called the value of 'x' changes from 1. Fix it so that 'x' is still 1 in the gobal scope.

<script>

      var x = 1;
      var y = 1;

      function hypotenuse(a , b) {
        var cSquared = a * a + b * b;
        x = Math.sqrt(cSquared);
        return x;
      }

      hypotenuse(x, y);


    </script>

I've got my screen capture of all the code in the video but I'm not seeing what I'm supposed to do. Everything typed in the video is already on here. Please help. TIA

5 Answers

Hi Jonathan,

What I posted above is the original code, I haven't done anything to it. I understand the shadowing and overwriting so X now comes out to 1.4142... Beyond that I don't understand where or what I'm supposed to change to get the global X to stay 1.

Edit: I just figured it out. Thanks for your help Jonathan!

Jon Wood
Jon Wood
9,884 Points

Anytime! Glad I could be of some assistance!

Jon Wood
Jon Wood
9,884 Points

I believe that you're overwriting "x" in your example. I'm not sure if you've gotten to the part about variable shadowing, but if you put "var x = Math.sqrt(cSquared);" then that should be what you need.

The function will have it's own scope of "x" and then after the function finishes executing it "x" will go back to the global variable you have set.

I hope that helps, but if you're still having issues, please let us know.

This helped a lot! Thank you :)

Emelin Burgos
Emelin Burgos
1,067 Points

This was so helpful, I was so confused with the shadowing

Thanks Jon Wood :-)

Try this is true var x = 1; var y = 1;

function hypotenuse(a , b) { var cSquared = a * a + b * b, x = Math.sqrt(cSquared); return x; }

hypotenuse(x, y);

When the 'hypotenuse' function is called the value of 'x' changes from 1. Fix it so that 'x' is still 1 in the gobal scope.

I think here you do not have to stress your self so much, you just need to Fix the x value by adding or inserting the variable in the local scale like this: x = Math.sqrt(cSquared); to var x = Math.sqrt(cSquared);