创建嵌套的if + else if语句,但函数返回为undefined

时间:2015-02-26 19:16:31

标签: javascript

我遇到此代码的问题,控制台声明该函数未定义。我似乎无法理解为什么。

<script>
    var month = new Date();
    var current_month = month.getMonth();
    var monthlist = ["January","February","March","April","May","June","July","August","September","October","November","December"];
    var YOY = 11;
    var org_traffic = 1,256;
    var HD = 12;
    var VIN = 30;
    var emails = 20;
    function analysis() {
        if (YOY >= 10) {
            if (HD >= 5) {
                if (VIN >= 5) {
                    if (emails >= 5) {
                        document.write("This month we saw that overall organic traffic has improved "+YOY+"% compared to last year, bringing in a total of "+org_traffic+" visits in "+(monthlist[current_month - 1])+", accompanied by the growth we're seeing with H&D visits, VIN views, and email leads!");
                    }
                    else {
                        document.write("This month we saw that overall organic traffic has improved "+YOY+"% compared to last year, bringing in a total of "+org_traffic+" visits in "+(monthlist[current_month - 1])+", accompanied by the growth we're seeing with H&D visits,and VIN views!");
                    } 
                }
                else if (VIN >= 0) {
                    document.write("This month we saw that overall organic traffic has improved "+YOY+"% compared to last year, bringing in a total of "+org_traffic+" visits in "+(monthlist[current_month - 1])+", accompanied by the growth we're seeing with H&D visits!<br/>We do see a modest "+VIN+"% growth in VIN views, which I'll be aiming to improve over the coming quarter.");
                }
                else if (VIN <= 0) {
                    document.write("This month we saw that overall organic traffic has improved "+YOY+"% compared to last year, bringing in a total of "+org_traffic+" visits in "+(monthlist[current_month - 1])+", accompanied by the growth we're seeing with H&D visits!<br/>Unfortuntately we did see a decrease in VIN views by about "+VIN+"%, which I'll be aiming to improve over the coming quarter.");
                }
                }
            else {
                document.write("This month we saw that overall organic traffic has improved "+YOY+"% compared to last year, bringing in a total of "+org_traffic+" visits in "+(monthlist[current_month - 1])+". Unfortunately our engagement metircs don't seem to be following suit and therefore will become a major focus of mine moving forward to turn our growing traffic numbers into an engaged visitor base.");
            }
            }
        else if (YOY >= 0) {
            document.write("This month we saw a modest "+YOY+"% growth in overall organic traffic compared to last year, bringing in a total of "+org_traffic+" visits. While any growth is definitely a positive, I would like to see this number improve over the coming quarter and will be taking proactive steps to turn up the dial on this trend.");
        }
        else if (YOY <= 0) {
            document.write("This month we saw a decrease in overall organic traffic compared to year by -"+YOY+"%. This is definitely not the trend I am looking to see from our efforts on the website. I'll be conducting a thorough investigation to see what factors may be causing this downward trend. I'll be sure to keep you up to date as I dig deeper into the root cause of this trend.");
        }
    }
    document.write(analysis());
    </script>


我喜欢任何可以尝试正确使用此功能的建议。

2 个答案:

答案 0 :(得分:4)

您的逗号var org_traffic = 1,256;应为var org_traffic = 1256;

修复无效的赋值,然后在命中函数定义之前代码不会中断。 :)

答案 1 :(得分:1)

有一些奇怪的事情在发生。

var org_traffic = 1, 256中的逗号不属于那里。

您的函数不返回字符串,因此您只需调用analysis()而不编写函数,因为函数返回值为undefined

控制台将始终为undefined记录document.write(),因为它也没有返回值。