Javascript预期表达式和缺少案例或语句错误

时间:2016-01-16 16:16:26

标签: javascript

我有两个错误 - "预期表达" (第3行)和"缺少案例或陈述错误" (第35行)。这是我的代码:



var days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30];

function month(number)
{
    var res = 0;

    for(i = 0; i < number; i++)
        res += days[i];  
        
        return res;  
}

function counter()
{
    var date = new Date();

    if((date.getFullYear() % 4) == 0 && (date.getFullYear() % 100) != 0)
        days[1] = 29;

    return (month(date.getMonth()) += date.getDate());
}

function season(day)
{
    if (day > month(2) + 20 && day <= month(5) + 21)
        return 0;
    else if (day > month(5) + 21 && day <= month(8) + 22)
        return 1;
    else if (day > month(8) + 22 && day <= month(11) + 21)
        return 2;

    return 3;
}

switch(season(counter()))
{
    case 0:
        document.getElementById("im").src = "wiosna.jpg";
        break;

    case 1:
        document.getElementById("im").src = "lato.jpg";
        break;

    case 2:
        document.getElementById("im").src = "jesień.jpg";
        break;

    case 3:
        document.getElementById("im").src = "zima.jpg";
        break;

    default:
        break;

}
&#13;
&#13;
&#13;

我不知道它为什么不起作用。当我删除第一个函数时,它指向下一个函数。有任何想法吗?谢谢你的帮助!

1 个答案:

答案 0 :(得分:1)

尝试替换

var ret = month(date.getMonth());
ret += date.getDate();
return ret

代表

return (month(date.getMonth()) += date.getDate());

以防止Uncaught ReferenceError: Invalid left-hand side in assignment错误

&#13;
&#13;
var days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30];

function month(number)
{
    var res = 0;

    for(i = 0; i < number; i++)
        res += days[i];  
        
        return res;  
}

function counter()
{
    var date = new Date();

    if((date.getFullYear() % 4) == 0 && (date.getFullYear() % 100) != 0)
        days[1] = 29;
  
    var ret = month(date.getMonth());
    ret += date.getDate();
    return ret;
}

function season(day)
{
    if (day > month(2) + 20 && day <= month(5) + 21)
        return 0;
    else if (day > month(5) + 21 && day <= month(8) + 22)
        return 1;
    else if (day > month(8) + 22 && day <= month(11) + 21)
        return 2;

    return 3;
}

switch(season(counter()))
{
    case 0:
        document.getElementById("im").textContent = "wiosna.jpg";
        break;

    case 1:
        document.getElementById("im").textContent = "lato.jpg";
        break;

    case 2:
        document.getElementById("im").textContent = "jesień.jpg";
        break;

    case 3:
        document.getElementById("im").textContent = "zima.jpg";
        break;

    default:
        break;

}
&#13;
<div id="im"></div>
&#13;
&#13;
&#13;