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.

7
  • I would try interests = [] instead of new Array() Commented Aug 30, 2014 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. Commented Aug 30, 2014 at 21:45
  • should always declare variables using var Commented Aug 30, 2014 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. Commented Aug 30, 2014 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. Commented Aug 30, 2014 at 21:51

1 Answer 1

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

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

Sign up to request clarification or add additional context in comments.

3 Comments

Yep. I missed that one too.. Without the == it's a infinite loop.
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!
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!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.