javascript 为什么JSFiddle不输出这段代码的结果
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19418308/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why doesn't JSFiddle output results for this code
提问by Trevor
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.)
我一直在使用 Firebug javascript 控制台来测试短脚本。有几个人建议改用 JSFiddle。问题是我似乎无法弄清楚如何做到这一点。我在 js 面板中输入我的代码并点击运行但没有任何反应。我假设某些东西应该输出到结果?我尝试了不同的设置,阅读 JSFiddle 文档,阅读 Stackoverflow 上发布的其他问题,但我无法弄清楚。好像应该这么简单。也许只有当我从 HTML 调用它时才有效?http://jsfiddle.net/nngrey/QgxCn/(我必须包含我的代码以引用指向 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);
回答by redOctober13
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.
你可以使用这个 jsFiddle 来输出带有 console.log 的东西。与 alert() 的想法相同,但没有弹出窗口,您必须关闭。感谢韦恩库尔特。
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);
回答by Trevor
Without any html to display your answer you can alert it to see the results.
没有任何 html 来显示您的答案,您可以提醒它查看结果。
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));