-1

A noob question. I am playing with http://www.w3resource.com/javascript-exercises/javascript-basic-exercises.php#EDITOR javascript exercises. Exercise number 4. Count the area of a triangle.

Why this works:

var a = 5;
var b = 6;
var c = 7;
var p = (a+b+c) / 2;
var p1 = (p * (p-a)*(p-b)*(p-c));
var poleA = Math.sqrt(p1);
console.log(poleA);

And this does not (console gives error):

function () {
var a = 5;
var b = 6;
var c = 7;
var p = (a+b+c) / 2;
var p1 = (p * (p-a)*(p-b)*(p-c));
var poleA = Math.sqrt(p1);
}
console.log(poleA);
Mac_W
  • 2,927
  • 6
  • 17
  • 30

1 Answers1

0

It could be because poleA is declared inside the function (in second case) and it does not exists in window namespace.

Try to remove var from poleAso it will become a global variable thus accessible outside of method scope.

Alex
  • 5,510
  • 8
  • 35
  • 54