2

I have a series of checkboxes, check to see that they've been checked, and if they have, add a string to an array. Then I loop through the array of strings and add the strings in the array to a new string separated by commas. Unfortunately, this always results in errors. In Chrome, the console reads "Invalid String Length". In Firefox, the error reads "allocation size overflow".

JavaScript:

// Compile List of Interests
interests = new Array();
if ( $("input[name=interestRoofing]").is(':checked') == true ) interests.push("Roofing");
if ( $("input[name=interestSiding]").is(':checked') == true ) interests.push("Siding");
if ( $("input[name=interestGutters]").is(':checked') == true ) interests.push("Gutters");
if ( $("input[name=interestWindows]").is(':checked') == true ) interests.push("Windows");
if ( $("input[name=interestPaint]").is(':checked') == true ) interests.push("Exterior Paint");
interestsString = "";
for ( i = 0; i < interests.length; i++ ) { // "Invalid String Length" in Chrome on This Line
    interestsString = interestsString + interests[i]; // "Allocation Size Overload" in Firefox on This Line
    if ( i < interests.length - 2 ) interestsString += ", ";
    if ( i = interests.length - 2 ) interestsString += "and ";
}

HTML:

<p>Interested In:</p>
<p><input type="checkbox" name="interestRoofing" value="">Roofing</p>
<p><input type="checkbox" name="interestSiding" value="">Siding</p>
<p><input type="checkbox" name="interestGutters" value="">Gutters</p>
<p><input type="checkbox" name="interestWindows" value="">Windows</p>
<p><input type="checkbox" name="interestPaint" value="">Exterior Paint</p>

I'm sure that whatever is going wrong will just hit me in the face when I see it, but I've been sitting on this problem all day and haven't come up with a solution yet. Obviously there's something here I'm not seeing. Any help would be much appreciated. Strangely, the console in Internet Explorer doesn't spit anything out, as if nothing went wrong. I can rid Chrome of the Invalid String Length error by adding an if statement to the beginning of the for loop checking to ensure that the length of the array is not 0. However, the error in Firefox remains, so that must not be the solution.

Michael Megee
  • 35
  • 1
  • 5
  • I would try `interests = []` instead of `new Array()` – Adem İlhan Aug 30 '14 at 21:42
  • The code seems to be correct. It may be related to your rest of code. Please check your rest of code too. – nisargjhaveri Aug 30 '14 at 21:45
  • should always declare variables using `var` – charlietfl Aug 30 '14 at 21:47
  • For some reason, [] instead of new Array() got rid of the error. But new Array() is the proper syntax, is it not? Thank you, though. I appreciate the help. – Michael Megee Aug 30 '14 at 21:47
  • With array literals, [] is actually a little faster. All modern JS VMs understand that [] means "new array literal". When you use "new Array()," it takes a couple extra cycles because the Array constructor is overloaded. In short-- you should use [], not new Array(). I found a great old answer on this [here](http://stackoverflow.com/a/7375418/1452497). – Antiga Aug 30 '14 at 21:51
  • Wait, no. I was wrong. That didn't fix the error at all. So, I tried going back and using the var keyword. Still no luck. I'm going to go ahead and inspect the rest of the code to see if the errors might be coming from elsewhere. In cases like this, I wish the console was a bit more informative. – Michael Megee Aug 30 '14 at 21:52
  • On another note, you absolutely should be declaring new variables with var. Without it, you are putting most of these objects in the global scope. – Antiga Aug 30 '14 at 22:01

1 Answers1

5
if ( i = interests.length - 2 ) interestsString += "and ";

You are doing an assignment here. You probably mean to be comparing.

Antiga
  • 2,264
  • 1
  • 20
  • 28
  • Yep. I missed that one too.. Without the `==` it's a infinite loop. – jwatts1980 Aug 30 '14 at 21:57
  • Oh lord, yes, it works! And it did hit me in the face, just as expected. In the future when I encounter this error, I'll know what it was. I almost wish I had posted this question sooner, but there was really no rush. I have weeks before this code is due. I really really appreciate the help, you have no idea. Thankyou, thank you, thank you! – Michael Megee Aug 30 '14 at 22:14
  • I'm happy that I could help. Please remember to mark answers or upvote those that were helpful in solving your problem. (Even if it wasn't mine!) Take care! – Antiga Aug 31 '14 at 00:54