Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem with an if-else if conditional statement

Status
Not open for further replies.

edwardglass57

Programmer
Mar 5, 2010
5
US
I have attempted several variations of this, none of which seem to help. I have narrowed this down to being the central problem at hand. Do i just have something out of place?

Code:
if (monthly_pay>=1000)                  
  {bank_account="Okay";}
else if ((monthly_pay>=700)&&(monthly_pay<=999))
  {bank_account="Still Okay";}
else if ((monthly_pay>=500)&&(monthly_pay<=699))
  {bank_account="Start to worry";}
else if ((monthly_pay>=300)&&(monthly_pay<=499))
  {bank_account="Panic";}
else if (monthly_pay<=299)
  {bank_account="Chapter 9";}
else
  {document.write("Marry Up")}
}

Optionally, could I not do else if and somehow use this as a Nested If?

Any help appreciated,
Thank You
 
Everything looks o.k

You do have a superfluous closing brace at the end, but I would assume that's just the closing brace for the function or something else not shown here.

If you are using IE use its error icon to see if there are any errors cropping up. If there's an error it will be on the bottom left side of the browser window. Looks like a yellow shield with an exclamation point.

Fiirefox has its Error console you can use.

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
I'd write it like this:

JavaScript:
monthly_pay = 999;
if (monthly_pay < 300) {
	bank_account = "Chapter 9";
} else if (monthly_pay < 500) {
	bank_account = "Panic";
} else if (monthly_pay < 700) {
	bank_account = "Start to worry";
} else if (monthly_pay < 1000) {
	bank_account = "Still Okay";
} else {
	bank_account = "Okay";
} 
document.write (bank_account);

That last IF for ("marry up") will never be reached in your original code.


--------

GOOGLE is a great resource to find answers to questions like "how do i..."


--------
 
Hi

Personally I prefer to user [tt]Array[/tt]s when possible. ( Ok, sometimes even when not possible... )
JavaScript:
[b]var[/b] what[teal]=[[/teal]
  [teal][[/teal][purple]1000[/purple][teal],[/teal][green][i]'Okay'[/i][/green][teal]],[/teal]
  [teal][[/teal][purple]700[/purple][teal],[/teal][green][i]'Still Okay'[/i][/green][teal]],[/teal]
  [teal][[/teal][purple]500[/purple][teal],[/teal][green][i]'Start to worry'[/i][/green][teal]],[/teal]
  [teal][[/teal][purple]300[/purple][teal],[/teal][green][i]'Panic'[/i][/green][teal]],[/teal]
  [teal][[/teal][purple]0[/purple][teal],[/teal][green][i]'Chapter 9'[/i][/green][teal]][/teal]
[teal]][/teal]

monthly_pay[teal]=[/teal][purple]999[/purple]

bank_account[teal]=[/teal][green][i]'Marry Up'[/i][/green]
[b]for[/b] [teal]([/teal][b]var[/b] i[teal]=[/teal][purple]0[/purple][teal],[/teal]l[teal]=[/teal]what[teal].[/teal]length[teal];[/teal]i[teal]<[/teal]l[teal];[/teal]i[teal]++)[/teal]
  [b]if[/b] [teal]([/teal]monthly_pay[teal]>=[/teal]what[teal][[/teal]i[teal]][[/teal][purple]0[/purple][teal]])[/teal] [teal]{[/teal]
    bank_account[teal]=[/teal]what[teal][[/teal]i[teal]][[/teal][purple]1[/purple][teal]][/teal]
    [b]break[/b]
  [teal]}[/teal]

document[teal].[/teal][COLOR=darkgoldenrod]write[/color][teal]([/teal]bank_account[teal])[/teal]
The advantage of separating the code and the data is the maintainability. For example if
[ul]
[li]You want to start worrying at another amount.[/li]
[li]You want to add more qualifiers or to remove some.[/li]
[li]You want to reuse the same amounts and qualifiers for other operations.[/li]
[li]You want to make the amounts and qualifiers editable by the user.[/li]
[li]You want to internationalize it.[/li]
[/ul]


Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top