/    Sign up×
Community /Pin to ProfileBookmark

Hi All,

I have a javascript function like this

[code]
for(var iPoint=0;iPoint<midStopPoint.length;iPoint=iPoint+3)
{
iPointArr = iPointArr + 2;
}
[/code]

Now I want to add an if statement in the increae ment operator .
Example.

If IPoint < midStopPoint.length then

iPoint=iPoint+3

else

iPoint=midStopPoint.length

How can I.

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@svidgenOct 20.2009 — I'm not sure I see why you need one. The [I]conditional[/I] is the second portion of the [I]for[/I] loop declaration. If you have some bizarre need that you're not letting onto here, you can try using the ternary operator.

If you don't like the ternary operator (like me), use a [I]while[/I] loop. For loops are nothing more than an alternate syntax for a variable declaration, increment statement, and while loop.

[code=html] for(var iPoint=0;iPoint<midStopPoint.length;iPoint=iPoint+3)
{
iPointArr = iPointArr + 2;
}[/code]

is equivalent to
[code=html]var iPoint=0;
while (iPoint<midStopPoint.length) {
iPointArr = iPointArr + 2;
iPoint=iPoint+3;
}[/code]
Copy linkTweet thisAlerts:
@JMRKEROct 20.2009 — My guess is that the OP is looking for the 'break' command ...

Consider this:
<i>
</i> iPointArr = 0; // should be initialized to something before use in FOR..LOOP
for (var iPoint=0;iPoint&lt;midStopPoint.length;iPoint=iPoint+3) {
if (iPoint &lt; midStopPoint.length) {
iPointArr = iPointArr + 2;
} else {
iPointArr = midStopPoint.length; break;
}
}

But it probably unnecessary for this particular script

as the stop condition is the same as the last loop of the FOR anyway.
Copy linkTweet thisAlerts:
@rnd_meOct 20.2009 — 
Now I want to add an if statement in the increae ment operator .

Example.

If IPoint < midStopPoint.length then

iPoint=iPoint+3

else

iPoint=midStopPoint.length

How can I.[/QUOTE]


you were close!
<i>
</i> for(var iPoint=0;iPoint&lt;midStopPoint.length;iPoint=iPoint+3)
{
iPointArr = iPointArr + 2;
}

[COLOR="Red"] iPoint=midStopPoint.length;[/COLOR]



now, ipoint will be midStopPoint.length as soon as it goes over the length, just like you specify.
Copy linkTweet thisAlerts:
@danasegaraneauthorOct 21.2009 — I'm not sure I see why you need one. The [I]conditional[/I] is the second portion of the [I]for[/I] loop declaration. If you have some bizarre need that you're not letting onto here, you can try using the ternary operator.

If you don't like the ternary operator (like me), use a [I]while[/I] loop. For loops are nothing more than an alternate syntax for a variable declaration, increment statement, and while loop.

[code=html] for(var iPoint=0;iPoint<midStopPoint.length;iPoint=iPoint+3)
{
iPointArr = iPointArr + 2;
}[/code]

is equivalent to
[code=html]var iPoint=0;
while (iPoint<midStopPoint.length) {
iPointArr = iPointArr + 2;
iPoint=iPoint+3;
}[/code]
[/QUOTE]



Thanks svidgen's .

I was looking for the ternary operator ?
×

Success!

Help @danasegarane 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 5.19,
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: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

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

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...