在if语句中的控制台日志中打印for循环

时间:2014-05-31 17:04:34

标签: javascript jquery

这是我的代码和JSFiddle

我无法使代码工作,我认为这是因为内部包含for loop的if语句。

如何使整个代码工作,我尝试了4种不同的代码可能性,但没有一种工作/

var commands = {
    'BOH': {
        text: 'BOH!',
    },
    'HALO': {
        text: 'HALO!',
    },
    'help()': {
        text: 'Please press F12 and see the value list.',
    },
};


$(".code").on("click", function () {
    var codePrompt = prompt("enter the value"),
    command = commands[codePrompt],
    alertMessage = "";
    consoleMessage = "Used '" + codePrompt + "' value.";
    if(!command) {
        alertMessage = "We are sorry but you entered a WRONG value.";
    } else {
        alertMessage = command.text;
    };
    if(command == 'help()') {
        for (key in commands){
            console.log(commands[key]);
            alertMessage = command.text;
        };
    };
alert(alertMessage);
console.log(consoleMessage);
});

1 个答案:

答案 0 :(得分:0)

不应该

if(command == 'help()') {

if(codePrompt == 'help()') {