Click here to Skip to main content
15,893,814 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
unable to run following js code
else part is not executing

What I have tried:

JavaScript
if ((m1 < 40) || (m2 < 40) || (m3 < 40) || (m4 < 40) || (m5 < 40) || (m6 < 40)) {
    result = "Fail";
    grade = "f";
}

else {
    var mm = avg;
    if (mm >= 80) {
        grade = "A";
        result = "Distinction";
    }
    elseif(mm >= 60)
    {
        grade = "B"
        result = "Firstclass";
    }
    elseif(mm >= 50)
    {
        grade = "C";
        result = "Secondclass";
    }
    elseif(mm >= 40)
    {
        grade = "D";
        result = "Pass";
    }
    elseif(mm < 40)
    {
        grade = "F";
        result = "Fail";
    }
}
Posted
Updated 19-Aug-20 1:13am
v2
Comments
Richard MacCutchan 19-Aug-20 3:03am    
One of those values (m1, m2 ...) must be less than 40. And where do you get the value of avg from?
Sanjay Taak 20-Aug-20 0:32am    
var total = (m1 + m2 + m3 + m4 + m5 + m6);
//var avg = (total / 600) * 100;
var avg = Math.round(total / 6);
[no name] 19-Aug-20 3:04am    
And what are the values for 'm1'...'m6' you are testing with?
Sanjay Taak 20-Aug-20 0:33am    
var m1 = parseInt(document.frmStudent.mark1.value);
var m2 = parseInt(document.frmStudent.mark2.value);
var m3 = parseInt(document.frmStudent.mark3.value);
var m4 = parseInt(document.frmStudent.mark4.value);
var m5 = parseInt(document.frmStudent.mark5.value);
var m6 = parseInt(document.frmStudent.mark6.value);
Sanjay Taak 20-Aug-20 0:34am    
values for m1m2.....is greater than 40 only

We can't run that code in isolation and get the same results you do - we have no access to the values you are using for testing.
So, it's going to be up to you.
Fortunately, you have a tool available to you which will help you find out what is going on: the debugger. How you use it depends on your browser, but a quick Google for the name and "debugger" should give you the info you need.
For Chrome, you will find lots of info here: Get Started with Debugging JavaScript in Chrome DevTools[^]
Otehr browsers wiull have similar facilities.

Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!
 
Share this answer
 
Quote:
unable to run following js code

We can't either because we don't know the value of the 7 variable the code depend on.
JavaScript
m1= ?; // Give sample values that lead to unexpected result
m2= ?;
m3= ?;
m4= ?;
m5= ?;
m6= ?;
avg= ?;
if ((m1 < 40) || (m2 < 40) || (m3 < 40) || (m4 < 40) || (m5 < 40) || (m6 < 40)) {
    result = "Fail";
    grade = "f";
}

else {
    var mm = avg;
    if (mm >= 80) {
        grade = "A";
        result = "Distinction";
    }
    elseif(mm >= 60)
    {
        grade = "B"
        result = "Firstclass";
    }
    elseif(mm >= 50)
    {
        grade = "C";
        result = "Secondclass";
    }
    elseif(mm >= 40)
    {
        grade = "D";
        result = "Pass";
    }
    elseif(mm < 40)
    {
        grade = "F";
        result = "Fail";
    }
}
 
Share this answer
 
It occurs to me that your problem is to do with using the wrong operator. You have the following:
JavaScript
if ((m1 < 40) || (m2 < 40) || (m3 < 40) || (m4 < 40) || (m5 < 40) || (m6 < 40)) {
    result = "Fail";
    grade = "f";
}

else {
    var mm = avg;
    // code removed for readability
}

This means that if any of those values are less than 40 then the result is a grade f fail. However, what if m2, m3,m4,m5 and m6 are all above 80? That would raise the average to a higher grade pass. So what I think you mean is:
JavaScript
if ((m1 < 40) && (m2 < 40) && (m3 < 40) && (m4 < 40) && (m5 < 40) && (m6 < 40)) {
    result = "Fail";
    grade = "f";
}
else {
    var mm = avg;
    // code removed for readability
}

Which makes more sense in that all the scores would need to be below 40 for a fail.
 
Share this answer
 
Comments
Patrice T 19-Aug-20 5:00am    
not necessary a wrong operator.
a fail if 1 value below 40 makes sense to me.
Richard MacCutchan 19-Aug-20 5:19am    
So if you have 89,82,84,85,80,32: average 76.6 you think that should be a fail? That makes no sense to me. In fact, according to the tests in the else clause, that would be a grade B First class pass.
Patrice T 19-Aug-20 5:30am    
I have seen such kind of rules.
Richard MacCutchan 19-Aug-20 5:35am    
But that is irrelevant.
Patrice T 19-Aug-20 5:37am    
only OP can tell us :)
When you set mm=avg and avg has not been declared then you have an error condition.

javaScript halts at that point - more than likely without an error message of any kind.

Also, wouldn't it make sense to calculate the avg before any test for the grades? Unless the entire grade depends upon any one test of the six (in most situations, quite unfair). Then the <40 can be taken care of in the switch and that block, testing each exam score for failure, can be removed. In fact, you already do this in your switch so you were expecting to get to that point, anyway !

So that gets rid of your 'if' and that means you also get rid of the else: it's just a switch that you need - and you need to calculate that value 'avg'. Also, if you already have the value for 'avg', why would you then copy it to mm and test it there? Just use 'avg' in the switch argument.

Another thing to consider: put your scores in an array. You can then have any number of scores for your exam and keep track of the student's status at any time without having to worry about handling zeroes for yet-undertaken exams.

All of his, of course, based upon the code you have shown, only
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900