What is wrong with this Javascript? shopping cart

旧巷老猫 提交于 2019-12-02 13:32:06

Your JS syntax is way off, this is what it should look like

function addItems(field) {
    for (i = 0; i <= field.length-1; i++) 
    {
        if (field[i].checked == true)
        {
            if (computer[i]!=null) { 
                selected[i] = computer[i];
            }
        }
    }
}

Half of your if statements are missing parentheses, that's some basic wrongfulness.

I don't know what and where should any of the variables be, but here is my best shot:

function addItems(field) {
    var i;
    for (i = 0; i < field.length; i++) {
        if (field[i].checked === true) {
            if (computer[i] !== null) { 
                selected[i] = computer[i];
            }
        }
    }
}

You are using i = 0 rather than var i = 0, which will introduce a global variable. This could be a problem if you're writing similar code elsewhere.

Your if-statements are not statements at all. They look like pseudo-code. You're also comparing with = rather than ==, which will cause an assignment rather than a condition, even if you fix up your syntax.

You are not properly indenting your code, which will make you much more prone to introduce new errors.

These are the general issues I notice immediately. Of course, heaps of things could be wrong with this code. fields might not be an array, computer and selected might not match the size of fields, etc.

If you have any specific problem, please describe that, and we may be able to address it.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!