/    Sign up×
Community /Pin to ProfileBookmark

JavaScript: For Loop: Why Doesn’t This Work? (Probably a Simple Solution…)

Hey guys, I’m just working on an assignment for a class.
The instructions require us to do checks on the user input before it is sent to the C++ script.
Well, I’m trying to use a for loop to run through all the user input boxes to use less space and keep it simpler instead of doing everything individually.
I never was very good with loops, so I can’t figure out what I’m doing wrong…

Here’s my code:

[CODE]
{
var val1 = document.getElementById(“cornPounds”).value,
val2 = document.getElementById(“wheatPounds”).value,
val3 = document.getElementById(“barleyPounds”).value,
val4 = document.getElementById(“oatsPounds”).value,
val5 = document.getElementById(“ryePounds”).value,
val = “”;
document.getElementById(“errorPlace”).innerHTML =””;
for (i=0; i<=5; i++)
{
if ((val+=i)=0 || isNaN((val+=i)))
{
document.getElementById(“errorPlace”).innerHTML = “All pound fields must be blank, or greater than 0 and a real number.”;
return false;
}
else
{
return true;
}
}
[/CODE]

Maybe something like this can’t be done this way, but to my mind it seems fine.
Anyone know how to fix this to make it work the way I want?
Firebug tells me it’s an invalid assignment on the left-hand side…

Thanks for any help,
Ryan

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@RyanJDauthorApr 05.2012 — Just looking at this, my code does look really stupid.

But I'm trying to get the "i" value on the back of "val" so the loop will work and take the value of variables val1-val5 and run the checks on them.

I'm definitely missing something...

?
Copy linkTweet thisAlerts:
@nitinmukeshApr 05.2012 — actually val+=i is wrong

It should be

parseInt((val+i))
Copy linkTweet thisAlerts:
@RyanJDauthorApr 05.2012 — Ah, of course. I should've known that the variable would have to be parsed to work,

but I'm getting the same error in Firebug. Could it be I have the parenthesis wrong?

I tried moving them:
[CODE]
if ((parseInt(val+i))=0 || isNaN((parseInt(val+i))))
[/CODE]


or is it:
[CODE]
if (parseInt((val+i))=0 || isNaN(parseInt((val+i))))
[/CODE]


D;
Copy linkTweet thisAlerts:
@nitinmukeshApr 05.2012 — for comparison replace = with ==

if((parseInt(val+i)==0)||(isNaN(parseInt(val+1))))[/QUOTE]
×

Success!

Help @RyanJD spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.3,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...