DirtyRedz
DirtyRedz

Reputation: 566

JavaScript for loop being skipped with no errors

I have here a block of JC code: $

 function Test() {
        var form = $('form').serializeArray();
        var str = "";
        var Price = {};
        var OneUp = 1;
        var NextName = "";
        var PriceIndex = 0
    for (var i = 0, l = form.length; i < l; i++) {
        OneUp = i + 1;
        if (form[i].name.indexOf("_Sel_") != -1) {
            NextName = form[OneUp].name.substring(0, form[OneUp].name.indexOf(":"));
            Price[PriceIndex] = form[OneUp].name.substring(form[OneUp].name.indexOf(":") + 1, form[OneUp].name.length);
            PriceIndex += 1;
            str += form[i].name.replace("_Sel_", "") + ':   <span class="ColorMe">' + NextName + "</span><br/>";
        }
    }

    for (var i = 0, l = Price.length; i < l; i++) {
        Price[i] = parseFloat(Price[i]) + .05;
    }
    var Total = 0.05;
    for (var i = 0, l = Price.length; i < l; i++) {
        Total += Price[i];
    }
    $(".PriceMe").html(Total.toString());
    $(".UpdateMe").html(str);

};

For no apparent reason or any errors this section of the code is being skipped:

for (var i = 0, l = Price.length; i < l; i++) {
    Price[i] = parseFloat(Price[i]) + .05;
}
var Total = 0.05;
for (var i = 0, l = Price.length; i < l; i++) {
    Total += Price[i];
}
$(".PriceMe").html(Total.toString());

Can anyone please explain to me why and or a fix to my current problem. Also i do apologize I am a novice JC coder, so any style or other issues you see in my code will be gladly accepted. Thxs.

Upvotes: 0

Views: 1014

Answers (3)

Johnny
Johnny

Reputation: 161

You forgot a semicolon after var PriceIndex = 0. There is no need to declare hundred variables in the for loop. You can just simply use for (var i = 0; i < Price.length; i++).

If you declare multiple variables after each other you can do this like this:

var a = 0, b = 1, c = 2;

You don't need to use var the whole time. The length method is only for arrays or html element collections. In order to get properties from an object you loop that object with an in keyword like this for (i in object) To access object you either use object.property or object["property"] if you do it like so object[property], javascript will think property is an variable, and is going to search for it and if it finds it javascript will return the value and start searching for in that object if the value will be other than a string, or the value will be not found in that object, javascript will return value undefined.

Edit:

form has a length property because jQuery searches DOM for that element, and it returns a html elements collection, which like an array has the length property.

Upvotes: 0

Anshul
Anshul

Reputation: 9480

Price.length is undefined at that point so use this :

  for (var i = 0, l = $(Price).length; i < l; i++) {
        Price[i] = parseFloat(Price[i]) + .05;
    }
    var Total = 0.05;
    for (var i = 0, l = Price.length; i < l; i++) {
        Total += Price[i];
    }
    $(".PriceMe").html(Total.toString());

and for access obj values you can use for(var key in obj).

Upvotes: 0

Adidi
Adidi

Reputation: 5253

Price is an object ({}) so it does not have the length property to go through object you need to do this for example:

var obj = {};
obj['key'] = 'value';
obj['key2'] = 'value2';
for(var val in obj){
   //val => key
   //obj[val] => value
}

Upvotes: 2

Related Questions