All you need to do to make this happen is redeclare the x variable using var
within the function. This is will declare the x
variable within the scope of the function, leaving the original, globally scoped x
variable untouched:
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);
Or, using the code style which you originally adopted (splitting out var
declarations):
var x = 1;
var y = 1;
function hypotenuse(a , b) {
var cSquared = a * a + b * b;
var x = Math.sqrt(cSquared);
return x;
}
hypotenuse(x, y);
For more detailed info on what is happening here, read up on javascript scope