1.

Solve : Javascript Input/Output math formula HELP?

Answer»

I have a workout program where people can post their scores for 4 different drills. The formula totals their score into a rating. I'd like to add a phrase afterwards that tells them how they did, but I have no clue how.<BR>

Here's my code:

Code: [SELECT]<html>
<head>
<title>Easy Workout</title>
<script language="javascript">
function addNumbers()
{
var val1 = parseInt(document.getElementById("value1").value);
var VAL2 = parseInt(document.getElementById("value2").value);
var val3 = parseInt(document.getElementById("value3").value);
var val4 = parseInt(document.getElementById("value4").value);
var ansD = document.getElementById("answer");
ansD.value = Math.round(((100-(25-val1)*5)/4 + (100-6.66667*(23-val2))/4 + (100-7.51*(15-val3))/4 + (100-4*(32-(val4)))/4)*100)/100 ;


}
 


</SCRIPT>
 
 
</head>
<body>
<table width=700><tr><td>XDrill = <INPUT type="text" id="value1" name="value1" value="" SIZE=4 /> </td><td>
2ptrs = <input type="text" id="value2" name="value2" value="" SIZE=4/> </td><td>
3ptrs = <input type="text" id="value3" name="value3" value="" SIZE=4/> </td><td>
Nash = <input type="text" id="value4" name="value4" value="" SIZE=4/></td><td>
<input type="button" name="Submit" value="Click here" onclick="javascript:addNumbers()"/> </td></tr><tr><td colspan=2>
<br><br>Your Score = <input type="text" id="answer" name="answer" value=""/ SIZE=4>
</body>
</html>

And this is what I'd like to add. I just have no clue how.

Code: [Select]function computeform(form) {
 
     
 
       if (answer >90) {
          form.my_comment.value="Amazing Job!!";
       }
 
       else if (answer >80 && answer <=90) {
          form.my_comment.value="You're doing very well.";
       }
 
       else if (answer >65 && answer <=80) {
          form.my_comment.value="Keep up the great work";
       }
 
       else if (answer >50 && answer <=65) {
          form.my_comment.value="You need to push it a little harder";
       }
 
       else if (answer >=35 && answer <=50) {
          form.my_comment.value="You can do better than that!";
       }
 
       else if (answer >=20 && answer <35) {
          form.my_comment.value="Poor performance";
       }
 
       else if (answer >=10 && answer <20) {
          form.my_comment.value="You must try harder than that";
       }
 
       else if (answer <10) {
          form.my_comment.value="You're definitely not giving it all your effort ";
       }
 
       }
       return;
}






Then I'd also like to add a clearform button and an alert if they've entered impossible numbers.

Code: [Select]function checkform(form) {
 
       if (value1==null||value1.length==0 || value2==null||value2.length==0 || value3==null||value3.length==0 || value4==null||value4.length==0){
            alert("\nPlease complete the form first");
            return false;
       }
 
       else if (parseFloat(value1) < 0||
                parseFloat(value1) >=40||
                parseFloat(value2) < 0||
                parseFloat(value2) >=40)
                parseFloat(value3) < 0||
                parseFloat(value3) >=40||
                parseFloat(value4) <= 0||
                parseFloat(value4) >=31){
                alert("\nDo you know what you're doing? \nPlease enter your scores again.");
                ClearForm(form);
                return false;
       }
       return true;
 
}


function ClearForm(form){
 
    form.weight.value = "";
    form.height.value = "";
    form.bmi.value = "";
    form.bmi.value = "";
    form.my_comment.value = "";
 
}
 

</SCRIPT>


Any help would be great. Thank you, in advance.Provided from what you already had, I just threw something together...now I didn't do any debugging or anything, so you may need to experiment a bit...

Code: [Select]<html>
<head>
<title>Easy Workout</title>
<script type="text/javascript" language="javascript">
function addNumbers()
{
var val1 = parseInt(document.getElementById("value1").value);
var val2 = parseInt(document.getElementById("value2").value);
var val3 = parseInt(document.getElementById("value3").value);
var val4 = parseInt(document.getElementById("value4").value);

var ansD = document.getElementById("answer");
ansD.value = Math.round(((100-(25-val1)*5)/4 + (100-6.66667*(23-val2))/4 + (100-7.51*(15-val3))/4 + (100-4*(32-(val4)))/4)*100)/100 ;

if ((val1==null || val1.length==0 || ansD.value=="NaN") && (val2==null || val2.length==0 || ansD.value=="NaN") && (val3==null || val3.length==0 || ansD.value=="NaN") && (val4==null || val4.length==0 || ansD.value=="NaN")){
            alert("\nPlease complete the form first");
            ClearForm();
            return false;
       }

else if ((parseFloat(val1) < 0 || parseFloat(val1) >=40) || (parseFloat(val2) < 0 || parseFloat(val2) >=40) || (parseFloat(val3) < 0 || parseFloat(val3) >=40) || (parseFloat(val4) <=0 || parseFloat(val4) >=31)){
         alert("\nDo you know what you're doing? \nPlease enter your scores again.");
         ClearForm();
         return false;
       }

       if (ansD.value >90) {
          form.my_comment.value="Amazing Job!!";
       }
 
       else if (ansD.value >80 && ansD.value <=90) {
          form.my_comment.value="You're doing very well.";
       }
 
       else if (ansD.value >65 && ansD.value <=80) {
          form.my_comment.value="Keep up the great work";
       }
 
       else if (ansD.value >50 && ansD.value <=65) {
          form.my_comment.value="You need to push it a little harder";
       }
 
       else if (ansD.value >=35 && ansD.value <=50) {
          form.my_comment.value="You can do better than that!";
       }
 
       else if (ansD.value >=20 && ansD.value <35) {
          form.my_comment.value="Poor performance";
       }
 
       else if (ansD.value >=10 && ansD.value <20) {
          form.my_comment.value="You must try harder than that";
       }
 
       else if (ansD.value <10) {
          form.my_comment.value="You're definitely not giving it all your effort ";
       }
 
       return;

}

function ClearForm(){
 
    document.form.value1.value = "";
    document.form.value2.value = "";
    document.form.value3.value = "";
    document.form.value4.value = "";
    document.form.answer.value = "";
    document.form.my_comment.value = "";
 
}
 
</SCRIPT>
 
 
</head>
<body>
<table width=700><form name="form"><tr><td>XDrill = <input type="text" id="value1" name="value1" value="" SIZE=4 /> </td><td>
2ptrs = <input type="text" id="value2" name="value2" value="" SIZE=4/> </td><td>
3ptrs = <input type="text" id="value3" name="value3" value="" SIZE=4/> </td><td>
Nash = <input type="text" id="value4" name="value4" value="" SIZE=4/></td><td>
<input type="button" name="Submit" value="Click here" onclick="javascript:addNumbers()"/> <input type="Reset" value="Clear"> </td></tr><tr><td colspan=2>
<br><br>Your Score = <input type="text" id="answer" name="answer" value=""/ SIZE=4 READONLY>
<br><br><br><input type="text" id="my_comment" name="my_comment" value=""/ SIZE=50 READONLY>
</form>
</body>
</html>Did this work for you?My finished product...


Code: [Select]<br>
<center><img src="http://i40.photobucket.com/albums/e250/jonsan32/easy.png"></center>
<br><script type = "text/javascript">
function addNumbers()
{
var val1 = parseInt(document.getElementById("value1").value);
var val2 = parseInt(document.getElementById("value2").value);
var val3 = parseInt(document.getElementById("value3").value);
var val4 = parseInt(document.getElementById("value4").value);
var ansD = document.getElementById("answer");
ansD.value = Math.round(((100-(25-val1)*5)/4 + (100-6.66667*(23-val2))/4 + (100-7.51*(15-val3))/4 + (100-4*(32-(val4)))/4)*100)/100 ;


}

function addNumbers()
{
var val1 = parseInt(document.getElementById("value1").value);
var val2 = parseInt(document.getElementById("value2").value);
var val3 = parseInt(document.getElementById("value3").value);
var val4 = parseInt(document.getElementById("value4").value);
var ansD = document.getElementById("answer");

var formula = Math.round(((100-(25-val1)*5)/4 + (100-6.66667*(23-val2))/4 + (100-7.51*(15-val3))/4 + (100-4*(32-(val4)))/4)*100)/100 ;

var phrase=new Array(" Yeah Right!"," A Legendary Performance"," You're an All-Star!"," Now that's college material!"," Very Good job."," Great Effort"," You're almost there!"," Nice try."," Average. Try harder."," Don't give up"," You've got a ways to go"," That's a poor performance");
if(formula>100){ ansD.value="You've somehow scored a " + formula +"." + phrase[0]}
if(formula<=100 && formula>=90){ ansD.value="You've scored a " + formula +"." + phrase[1]}
if(formula<90 && formula>=80){ ansD.value="You've scored a " + formula +"." + phrase[2]}
if(formula<80 && formula>=70){ ansD.value="You've scored a " + formula +"." + phrase[3]}
if(formula<70 && formula>=60){ ansD.value="You've scored a " + formula +"." + phrase[4]}
if(formula<60 && formula>=50){ ansD.value="You've scored a " + formula +"." + phrase[5]}
if(formula<50 && formula>=40){ ansD.value="You've scored a " + formula +"." + phrase[6]}
if(formula<40 && formula>=30){ ansD.value="You've scored a " + formula +"." + phrase[7]}
if(formula<30 && formula>=20){ ansD.value="You've scored a " + formula +"." + phrase[8]}
if(formula<20 && formula>=10){ ansD.value="You've scored a " + formula +"." + phrase[9]}
if(formula<10 && formula>=0){ ansD.value="You've scored a " + formula +"." + phrase[10]}
if(formula<0){ ansD.value="You've scored a " + formula +"." + phrase[11]}

}
 


</SCRIPT>
 
 
 <center>
<table width=700><tr><td>XDrill = <input type="text" id="value1" name="value1" value="" SIZE=2 /> <br><br></td><td>
2ptrs = <input type="text" id="value2" name="value2" value="" SIZE=2/><br><br> </td><td>
3ptrs = <input type="text" id="value3" name="value3" value="" SIZE=2/><br><br> </td><td>
Nash = <input type="text" id="value4" name="value4" value="" SIZE=2/><br><br></td><td>
<input type="button" name="Submit" value="Click here" onclick="javascript:addNumbers()"/><br><br> </td></tr><tr><td colspan=5 bgcolor="#000000"><center>
<input type="text" id="answer" name="answer" value=""/ SIZE=75> </center></td></tr></table>
<br><br></center>



<table width="700" border="1" cellpadding=6><tbody>

<tr><td bgcolor="#eeeeee">
<div align="justify"><font size=4 face="garamond"><b>Got 15 minutes?</b></font><font size=2 face="garamond"> Do the following 4 drills, then enter your scores above. Switch up the order any way you desire to maximize your results. Do AT LEAST this much every day if you want to be good. 3 MINUTES per drill, with a ONE MINUTE rest in between each. If you can't spare 15 minutes per day, you just don't want it bad enough!</font></div>
</td></tr>

<tr><td onmouseover="this.bgColor='#fff000'" onmouseout="this.bgColor='white'" bgcolor="white"><table width="100%" cellpadding=5><tbody><tr><td width="50"><img src="http://i40.photobucket.com/albums/e250/jonsan32/letter-x-red.jpg" width="50" height="50" /></td><td width="100"><b><font color="#000000">X Drill</b></td><td><small><font color="#000000"><p align="justify">Sprint-dribbling jumpshots from each of the 4 elbows, running full court in an X pattern (3 minutes). Run straight, then cross, run straight, then cross, repeat...</small></td></tr></tbody></table></td></tr></font></font>


<tr><td onmouseover="this.bgColor='#fff000'" onmouseout="this.bgColor='white'" bgcolor="white"><table width="100%"  cellpadding=5><tbody><tr><td width="50"><img src="http://i40.photobucket.com/albums/e250/jonsan32/images-2.jpg" width="50" height="50" /></td><td width="100"><b><font color="#000000">2-Pointers</b></td><td><small><font color="#000000"><p align="justify">How many mid-range jumpshots off the dribble you can make in 3 minutes.</small></td></tr></tbody></table></td></tr></font></font>


<tr><td onmouseover="this.bgColor='#fff000'" onmouseout="this.bgColor='white'" bgcolor="white"><table width="100%" cellpadding=5><tbody><tr><td width="50"><img src="http://i40.photobucket.com/albums/e250/jonsan32/threepointline.jpg" width="50" height="50" /></td><td width="100"><b><font color="#000000">3-Pointers</b></td><td><small><font color="#000000"><p align="justify">How many 3-pointers you can make in 3 minutes.</small></td></tr></tbody></table></td></tr></font></font>




<tr><td onmouseover="this.bgColor='#fff000'" onmouseout="this.bgColor='white'" bgcolor="white"><table width="100%"  cellpadding=5><tr><td width="50"><img src="http://i40.photobucket.com/albums/e250/jonsan32/nash.jpg" width="50" height="50" /></td><td width="100"><b><font color="#000000">Nash Drill</b></td><td><small><font color="#000000"><p align="justify">Sprinting, down 'n back is 2. See how many you can do in 3 minutes, Steve Nash can do 30!</small></td></tr></tbody></table></font></font></td></tr></table>


Thanks



Discussion

No Comment Found