/    Sign up×
Community /Pin to ProfileBookmark

if code embedded in if code

I’m having syntax troubles with the following code. I think the issue is that the second if clause is not properly embedded in the first if clause.

What’s happening is a link is clicked on to display either full time or part time employees. Fulltime or parttime is passed up as an argument of function displayEmployees.

The program runs a for loop through an array of employees. If the argument of the function is “part-time”, then the code says, if the array object’s status is part-time, select its details and return them in an alert message. If the argument of the function is “full-time”, then the code says, if the array object’s status is full-time, select its details and return them in a different alert message.

So the following code returns a syntax error indicating a problem with “else” clause. Any ideas why?

[code]

function displayEmployees(time) {
var messagefulltime = ”;
var messageparttime = ”;
[B]for [/B](var i = 0 ; i < eList.length ; i++) {
[B]if[/B] (time == ‘parttime’) {
[B]if [/B](eList[i].status = ‘Part-time’) {
messageparttime += eList[i].name;
messageparttime += ‘, salary: ‘ + eList[i].salary;
messageparttime += ‘, age: ‘ + eList[i].age;
messageparttime += ‘, status: ‘ + eList[i].status;
messageparttime += ‘. n’;
}
}
alert(messageparttime);

[B]else [/B]{
messagefulltime += eList[i].name;
messagefulltime += ‘, salary: ‘ + eList[i].salary;
messagefulltime += ‘, age: ‘ + eList[i].age;
messagefulltime += ‘, status: ‘ + eList[i].status;
messagefulltime += ‘. n’;
}
alert(messagefulltime);
}
}
[/code]

Thanks,

Si

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@aj_nscNov 06.2008 — You've got a break between your if and your else (where you alert 'parttime'). This isn't allowed. An if-else statement is first if, then else, no in between. Put that alert somewhere else and it wont give you an error anymore.


Also, you the code could be written in an (arguably) better way. If you are just embedding one single if statement (with no corresponding else) inside another if statement, then use the logical AND operator (&&) in the first if statement.

<i>
</i>function displayEmployees(time) {
var messagefulltime = '';
var messageparttime = '';
for (var i = 0 ; i &lt; eList.length ; i++) {
if (time == 'parttime' &amp;&amp; eList[i].status = 'Part-time') {
messageparttime += eList[i].name;
messageparttime += ', salary: ' + eList[i].salary;
messageparttime += ', age: ' + eList[i].age;
messageparttime += ', status: ' + eList[i].status;
messageparttime += '. n';
} else {
messagefulltime += eList[i].name;
messagefulltime += ', salary: ' + eList[i].salary;
messagefulltime += ', age: ' + eList[i].age;
messagefulltime += ', status: ' + eList[i].status;
messagefulltime += '. n';
}

<i> </i> alert(messageparttime);
<i> </i> alert(messagefulltime);
<i> </i> }

}
Copy linkTweet thisAlerts:
@stjohnsiauthorNov 06.2008 — Thanks for that. Having tried it though, I now get two different alert messages coming up, since that's what your programming returns. Is there any way of the if clause returning an alert message, without that alert message being embedded in the if... else construct, and so undermining it?

Thanks
Copy linkTweet thisAlerts:
@ZeroKilledNov 06.2008 — would not it work for you as follow?
<i>
</i> if (time == 'parttime' &amp;&amp; eList[i].status [color='red']==[/color] 'Part-time') {
messageparttime += eList[i].name;
messageparttime += ', salary: ' + eList[i].salary;
messageparttime += ', age: ' + eList[i].age;
messageparttime += ', status: ' + eList[i].status;
messageparttime += '. n';
alert(messageparttime);
} else {
messagefulltime += eList[i].name;
messagefulltime += ', salary: ' + eList[i].salary;
messagefulltime += ', age: ' + eList[i].age;
messagefulltime += ', status: ' + eList[i].status;
messagefulltime += '. n';
alert(messagefulltime);
}
×

Success!

Help @stjohnsi 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.26,
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,
)...