Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't JSFiddle output results for this code

I have been using the Firebug javascript console to test short scripts. Several people have suggested using JSFiddle instead. The problem is I can't seem to figure out how to do this. I enter my code in the js panel and hit run but nothing happens. I am assuming something should output to results? I tried different settings, reading the JSFiddle documentation, reading other questions posted on Stackoverflow, but I can't figure it out. It seems like it should be so simple. Maybe it only works if I call it from HTML? http://jsfiddle.net/nngrey/QgxCn/ (I had to include my code to reference the link to JSFiddle.)

function Palindrome(str) {
    str = str.split("");
    for (var i = 0; i < str.length; i++) {
        if (str[i] === " ") {
            str.splice(i, 1);
        }
    }
    revStr = str.reverse().join("");
    str = str.join("");
    if (revStr === str) {
        return true;
    } else {
        return false;
    }
    return str;
}

str = "dont nod";
Palindrome(str);

2 Answers

You can use this jsFiddle to output stuff with console.log. Same idea as alert(), but without a popup you have to close. Thanks to Wayne Koort.

http://jsfiddle.net/TEHLb/

var consoleLine = "<p class=\"console-line\"></p>";
console = {
    log: function (text) {
        $("#console-log").append($(consoleLine).html(text));
    }
};
var myVar = "foo";
console.log('Your variable has the value ' + myVar);
like image 177
redOctober13 Avatar answered Sep 15 '26 19:09

redOctober13


Without any html to display your answer you can alert it to see the results.

function Palindrome(str) {
  str = str.split("");
  for (var i = 0; i < str.length; i++) {
    if (str[i] === " ") {
        str.splice(i, 1);
    }
}
revStr = str.reverse().join("");
str = str.join("");
if (revStr === str) {
    return true;
} else {
    return false;
}
    return str;
}

str = "dont nod";
alert(Palindrome(str));
like image 33
Trevor Avatar answered Sep 15 '26 20:09

Trevor