3

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);

3 Answers3

7

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);
redOctober13
  • 3,662
  • 6
  • 34
  • 61
5

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));
Trevor
  • 16,080
  • 9
  • 52
  • 83
  • Also your `return str;` will never get returned because of the `return false;` and `return true;` before it. The function stops running once its returned ie It only returns once result. – Trevor Oct 17 '13 at 04:35
  • Thanks! Do you understand the difference between the Firebug's Javascript console, Chrome's Javascript console, and JSFiddle's interface? That is, when I type into the righthand side of Firebug's console, I get all kinds of feedback. Not so much with Chrome and JSFiddle. I feel like I am not using these two correctly. There must be more that I am missing. –  Oct 17 '13 at 04:48
  • 1
    Check out this question http://stackoverflow.com/questions/14328963/access-variables-in-jsfiddle-from-javascript-console if I open up your fiddle and then in the `Chrome` java script console I change `` to `result(fiddle.jshell.net/)` and on the top left if you change `onLoad` to `No Wrap - in ` you can then re run the fiddle and you can access the functions and variables in the console `console.log(Palindrome(str));` works. This seems to be unique to JSFiddle though I have never had trouble accessing variables from the console on local development. – Trevor Oct 17 '13 at 12:22
1

you need alert(Palindrome(str));

try this demo

iJade
  • 23,144
  • 56
  • 154
  • 243