hi guys im a newbie on html java script,,
im using switch statement but my final output always fall at my default statement
need help....

here's my code...

function finalgrade()
{
    var texttgp, texttgm, texttgsf, texttgf, textfg;

    texttgp = parseFloat(document.frmMacky.textTgp.value);
    texttgm = parseFloat(document.frmMacky.textTgm.value);
    texttgsf = parseFloat(document.frmMacky.textTgsf.value);
    texttgf = parseFloat(document.frmMacky.textTgf.value);
    textfg = (texttgp*.20)+(texttgm*.20)+(texttgsf*.20)+(texttgf*.40);

    document.frmMacky.textFg.value = textfg.toFixed(2);

    switch(textfg)
    {
    case(textfg >=95):
    document.frmMacky.textEqvlnt.value="1.0";
    break;
    case(textfg ==94):
    document.frmMacky.textEqvlnt.value="1.1";
    break;
    case(textfg ==93):
    document.frmMacky.textEqvlnt.value="1.2";
    break;
    case(textfg ==92):
    document.frmMacky.textEqvlnt.value="1.3";
    break;
    case(textfg ==91):
    document.frmMacky.textEqvlnt.value="1.4";
    break;
    case(textfg ==90):
    document.frmMacky.textEqvlnt.value="1.5";
    break;
    case(textfg ==89):
    document.frmMacky.textEqvlnt.value="1.6";
    break;
    case(textfg ==87):
    document.frmMacky.textEqvlnt.value="1.8";
    break;
    case(textfg ==86):
    document.frmMacky.textEqvlnt.value="1.9";
    break;
    case(textfg ==85):
    document.frmMacky.textEqvlnt.value="2.0";
    break;
    case(textfg >=82 && textfg <=84):
    document.frmMacky.textEqvlnt.value="2.25";
    break;
    case(textfg >=81 && textfg <=82):
    document.frmMacky.textEqvlnt.value="2.5";
    break;
    case(textfg >=76 && textfg <=79):
    document.frmMacky.textEqvlnt.value="2.75";
    break;
    case(textfg ==75):
    document.frmMacky.textEqvlnt.value= "3.0";
    break;
    default:
    document.frmMacky.textEqvlnt.value= "Incomplete";
    }

Recommended Answers

All 4 Replies

I would assume you are getting a NaN as a result of one or all of your parseInt() calls. A number & NaN is NaN which is a failure in your assertion in the switch.

I may just be ignorant of the syntax, but I am not sure what document.frmMacky is.

I am taking, by the context, that you have only one form on the page, with multiple inputs that accept numbers. In that case, you may want to consider using this syntax (the HTML is psudo code, of course):

<form...>
  <input name="foo" value="" type="text" maxlen="3" />
</form>

document.forms[0].elements["foo"].value;

I would even encourage you to break apart the form portion, and use locals so you can do error checking and non-existent variable checks (which will help to reduce those NaN errors by allowing you to replace NaN with 0 or a default value).

To me, this line is error prone:
textfg = (texttgp*.20)+(texttgm*.20)+(texttgsf*.20)+(texttgf*.40);

You have no checking to see what any of those variables are, and you assume way too much. All of those should be checked individually prior to, or during, the addition of their individual values.

Edit: turns out, since I just tried it, you can call a form by its name. Never tried before... not sure how cross browser it is, but that may be just because I don't use that syntax.

Therefore, I stand by my original guess, that you are getting a NaN value.

The 'switch' statement generally uses the 'case' operator to test equality, so when your code tests 'textfg' against the comparisons in your 'case' functions, it's always falling through to the default because 'textfg' never evaluates to 1 or 0 (true or false).

This might not work for your code, but there is a well-known hack to allow the use of a code structure like you have. Try:

switch (true) {

Otherwise, I think you'll have to use multiple 'if's.

good catch rtrethewey :)

I guess it really does help to read all the words... :-/

try parseFloat on the textfg variable

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.