-4

If someone could check out http://jsfiddle.net/fdzVq/ and lend me an extra set of eyes, that would be amazing. The div is supposed to change to one of the supplied messages, but it doesn't do anything.

JS:

function getPowerMsg(kj){
var kwh = kj * 0.00027777777777778;
var stuff = [["an LED night light", 0.5],["a clock radio", 4],["a night light", 5]];        
for(var i=0; i<stuff.length; i++){
var item = stuff[i][0];
var watts = stuff[i][1];
var kwatts = watts/1000;
            var hours = (Math.floor((kwh/kwatts)*2)/2);
            if(hours > 0){
                results.push([item, convertTime(hours)]);
            }
        }
        return results[randbetween(0,results.length-1)];
}

function convertTime(hrs){
        var msg = "";
        if(hrs < 24){
            msg = hrs+" hour";
            if(hrs != 1){ msg += "s"; }
            if(hrs < 1){ msg = "a half hour"; }
        }else if(hrs == 24){
            msg = "a full day";
        }else if(hrs < 33){
            msg = "over a day";
        }else if(hrs < 39){
            msg = "about a day and a half";
        }else if(hrs < 48){
            msg = "almost 2 days";
        }else if(hrs == 48){
            msg = "2 days";
        }else if(hrs < 60 ){
            msg = "over 2 days";
        }else if(hrs < 72){
            msg = "almost 3 days";
        }else if(hrs == 72){
            msg = "3 days";
        }else{
            msg = "over 3 days";
        }
        return msg;
}

function changePowerMsg(num){
    var x = document.getElementById("powermsg");
    var y = getPowerMsg(num);
    x.innerHTML = y;
}

HTML:

<div id="powermsg">Hi</div>
<a href="javascript:;" onclick="javascript: changePowerMsg(200);">Change!</a>
j08691
  • 204,283
  • 31
  • 260
  • 272
Dan
  • 121
  • 2
  • 10

1 Answers1

1

Couple of things.

  • jsFiddle:Change to nowrap in
  • results is undefined because it is not declared
  • randbetween is undefined because it is not declared
  • You dont have to specify javascript: in the onclick or href.
  • This is a slightly cleaned up version JS

In general you should remember your javascript debuggers that are built into your browser as this is the sort of thing that they are there for and, that most modern browsers supply this for you. The following is not an exhausted list but with this you should get the general idea.

  • Access Debugger in IE
  • Access Debugger in FF
  • Access Debugger in Chrome
  • Terrance
    • 11,764
    • 4
    • 54
    • 80