Stack Exchange
Javascript Code Not Working for two different Values
My Javascript Code Not Working for two different Values, where is the problem?
I'm trying to set the code to divide with two different values in two different conditions. But I'm in the if else statement. Code link here: https://jsfiddle.net/gLb0uok5/


function calc()
    {
        var m1,m2,m3,avg = 0,total = 0, result = "",grade = "";
        m1 = parseInt(document.form1.wp.value);
        m2 = parseInt(document.form1.sp.value);
        m3 = parseInt(document.form1.cg.value);
        total = m1+m2+m3;
       
        
        if( m3 = 0)
        {
            avg = total/2;
        }
        else(m3 >= 1)
        {
            avg = total/3;
        }
        document.form1.result.value = result;
        document.form1.grade.value = grade;
        document.form1.total.value = total;
        document.form1.average.value = avg;
        
    }
<form name = "form1">
        <table border = "1">
            <tr>
                <td> Student Name</td>
                <td><input type = "text"  /></td>
            </tr>
            <tr>
                <td colspan = "2" align = "center">Subject Marks</td>
            </tr>
            <tr>
                <td>Web Programming</td>
                <td><input type = "text" name = "wp" /></td>
            </tr>
            <tr>
                <td>Computer Graphics</td>
                <td><input type = "text" name = "cg" /></td>
            </tr>
            <tr>
                <td>System Programming</td>
                <td><input type = "text" name = "sp" /></td>

            </tr>
            <tr>
                <td colspan = "2" align = "center"><input type = "button"   onclick = "calc()" value = "calculte" /></td>
            </tr>            
            <tr>
                <td>Total</td>
                <td><input type = "text" name = "total"/></td>

            </tr>
            <tr>
                <td>Average</td>
                <td><input type = "text" name = "average" /></td>
            </tr>
            <tr>
                <td>Result</td>
                <td><input type = "text" name = "result" /></td>
            </tr>
            <tr>
                <td>Grade</td>
                <td><input type = "text" name = "grade"/></td>
            </tr>

        </table>
    </form>




Please explain your problem more neatly with code

You have syntax errors. And you should check if m3 exists before adding it to total.

when input is not given, m1 or m2 or m3 will be NaN (Not a Number)
So just do the check:
    if(isNaN(m1)) m1 =0;
    if(isNaN(m2)) m2 =0;
    if(isNaN(m3)) m3 =0;

It will work. Working code here

You just forgot to set values to the elements and trying to access the values, which will be always NULL, that's why you are getting NaN error. Please see the working demo here. I am just adding value="2".


function calc()
    {
        var m1,m2,m3,avg = 0,total = 0, result = "",grade = "";
        m1 = parseInt(document.form1.wp.value);
        m2 = parseInt(document.form1.sp.value);
        m3 = parseInt(document.form1.cg.value);
        total = m1+m2+m3;       
        
        if( m3 = 0)
        {
            avg = total/2;
        }
        else(m3 >= 1)
        {
            avg = total/3;
        }
        document.form1.result.value = result;
        document.form1.grade.value = grade;
        document.form1.total.value = total;
        document.form1.average.value = avg;        
}
<form name = "form1">
        <table border = "1">
            <tr>
                <td> Student Name</td>
                <td><input type = "text"  /></td>
            </tr>
            <tr>
                <td colspan = "2" align = "center">Subject Marks</td>
            </tr>
            <tr>
                <td>Web Programming</td>
                <td><input type = "text" name = "wp" value="2" /></td>
            </tr>
            <tr>
                <td>Computer Graphics</td>
                <td><input type = "text" name = "cg" value="2" /></td>
            </tr>
            <tr>
                <td>System Programming</td>
                <td><input type = "text" name = "sp"  value="2" /></td>

            </tr>
            <tr>
                <td colspan = "2" align = "center"><input type = "button"   onclick = "calc()" value = "calculte" /></td>
            </tr>            
            <tr>
                <td>Total</td>
                <td><input type = "text" name = "total"/></td>

            </tr>
            <tr>
                <td>Average</td>
                <td><input type = "text" name = "average" /></td>
            </tr>
            <tr>
                <td>Result</td>
                <td><input type = "text" name = "result" /></td>
            </tr>
            <tr>
                <td>Grade</td>
                <td><input type = "text" name = "grade"/></td>
            </tr>

        </table>
    </form>



