What Is Wrong With This Javascript? Shopping Cart
Solution 1:
Your JS syntax is way off, this is what it should look like
functionaddItems(field) {
for (i = 0; i <= field.length-1; i++)
{
if (field[i].checked == true)
{
if (computer[i]!=null) {
selected[i] = computer[i];
}
}
}
}
Solution 2:
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:
functionaddItems(field) {
var i;
for (i = 0; i < field.length; i++) {
if (field[i].checked === true) {
if (computer[i] !== null) {
selected[i] = computer[i];
}
}
}
}
Solution 3:
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.
Post a Comment for "What Is Wrong With This Javascript? Shopping Cart"