if语句不警告变量

时间:2017-08-30 19:49:35

标签: javascript if-statement alert

<html>
<head>
<title></title>
<script src="https:/code.jquery.com/jquery-3.2.1.js"></script>
</head>
<body>
    <button>Button</button>
    <script>
        var user_response='';
        $('button').on('click',ask_a_question);

        function ask_a_question(){

            user_response = prompt('What is your item name?').toLowerCase();

            if (user_response === 'apple')

//Between here is where i dont understand why, 
//when the if statement is true, that it does not "alert" the bucketlist
//variable and then tells the user **"'learn to juggle', 'take a falconry
//class', 'climb mt everest'."** Does this code even make sense?

            alert(bucketList);

            var bucketList = [
                'learn to juggle', 
                'take a falconry class', 
                'climb Mt Everest'
            ];

        }
    </script>
</body>
</html>

2 个答案:

答案 0 :(得分:4)

您正在尝试在定义bucketList之前显示它们的值。

尝试移动

var bucketList = [
    'learn to juggle', 
    'take a falconry class', 
    'climb Mt Everest'
];

以上alert(bucketList);

答案 1 :(得分:1)

当我按照编写的方式运行代码时,我会收到警告“未定义”。

如果我将警报移动到定义了bucketlist之后,我会收到显示数组内容的警告。

相关问题