0

Another undefined error that I cant seem to get my head around. When I run the code my results include a undefined line:

No fault 06:44:46
Pkey 1 undefined 06:44:44
Pkey 1 Left 06:44:37
No fault Feb 09 12:28
Pkey 1 undefined Feb 09 12:28
Pkey 1 Right Feb 09 12:28 

Please could someone point me in the right direction. Thanks

var a=p1.value;
var le=p9.value;
var re=p10.value;
if (le==true) {
            var x = " Left";
}
else if (re==true) {
            var x = " Right";

if (a>0) {
            var y = "Pkey"; 
            var result = y +" "+ a +" "+ x;
}
else if (a==0) {
            var result = "No fault"
return result;
Duncan
  • 3
  • 2

2 Answers2

3

If neither the value of le or re evaluates to true, then x would be undefined.

As you only use == for comparison, remember that values like 0, null, empty string, and []all evaluate to false.

kaspermoerch
  • 16,127
  • 4
  • 44
  • 67
-2

The x should be declared before the statements.. and not inside the 'if' scopes

ItayB
  • 10,377
  • 9
  • 50
  • 77
  • That's good coding style, but makes no difference from JavaScript's perspective. – Quentin Feb 10 '14 at 11:51
  • Well it was worth to be down-voted because I had the opportunity to learn something new (actually js is not my native..) - just finished reading this related [issue about javascript variable scope](http://stackoverflow.com/questions/500431/javascript-variable-scope). Anyway as a developer in many programming languages it was the first think that catch my eyes and I think it might be helpful to stick the tradition of good coding style / readability code - especially when this is an enforcement in some other common programming languages. – ItayB Feb 11 '14 at 12:44