Closed Thread Icon

Topic awaiting preservation: nested if then statement (Page 1 of 1) Pages that link to <a href="http://ozoneasylum.com/backlink?for=24827" title="Pages that link to Topic awaiting preservation: nested if then statement (Page 1 of 1)" rel="nofollow" >Topic awaiting preservation: nested if then statement <span class="small">(Page 1 of 1)</span>\

 
Seymour
Bipolar (III) Inmate

From: K-town, FL, USA
Insane since: Jun 2002

posted posted 01-28-2005 17:37

I am having some trouble getting the errors out of my javascript... I have googled for it and found a couple of examples but none that seemed to help me. If anyone can help me it would be greatly appreciated.
<html>
<head><title></title></head>
<body>
<script language="JavaScript">

var NumGrade; var AlphGrade;
NumGrade=52;

if (NumGrade > 89){
AlphGrade = "A";
else
if (NumGrade > 79) {
AlphGrade = "B";

else
if (NumGrade > 69) {
AlphGrade = "C";

else
if (NumGrade > 59) {
AlphGrade = "D";

else
AlphaGrade = "F";
}
}
}
}



document.write("Your grade for this course is " + AlphGrade);

</script>
</body>
</html>

hyperbole
Paranoid (IV) Inmate

From: Madison, Indiana, USA
Insane since: Aug 2000

posted posted 01-28-2005 18:11
code:
var NumGrade; var AlphGrade;
NumGrade=52;

if (NumGrade > 89)
AlphGrade = "A";
else if (NumGrade > 79)
AlphGrade = "B";
else if (NumGrade > 69)
AlphGrade = "C";
else if (NumGrade > 59)
AlphGrade = "D";
else
AlphaGrade = "F";




When you write create an if with brackets, you need to complete the brackets statment before the else.

code:
if  (...)
{
....
}
else
{
...
}






.

-- not necessarily stoned... just beautiful.

Cameron
Paranoid (IV) Inmate

From: Brisbane, Australia
Insane since: Jan 2003

posted posted 02-07-2005 05:59

Hyperbole's example might be a tad confusing, I'd always better to use the curly braces {} regardless, like so...

code:
if (a > b) {
a = b;
b = b + 1;
} else if (a < b) {
b = a;
a = a - 1;
} else {
a = 0;
b = 0;
}



When coding it's handy to remember that things like braces or quotes always go in pairs. One starts something { and the other must end it }. In Javascript you don't actually "need" the braces for simple one liners and the like, but it's considered good from to include them none the less.

« BackwardsOnwards »

Show Forum Drop Down Menu