mercredi 16 décembre 2015

Using if and else if functions with select inputs (converting results based on km and miles)

I have been trying to build a race predictor calculator based off of Peter Riegel's formula for a couple of days now and have only managed to get it working when the original and future distances are set the same (See below).

However I would like for the user to be able to select kilometres or miles using 'select' and 'options' in the form for both the original and future distance. I have played around with else and else if statements in the script but have completely failed at this. I would greatly appreciate it if someone could help me crack this. (I have put the select and options in already so it is just the scripting that I need help with).

I am completely self taught so I apologise if the code below is a complete mess or incorrect. Thank you! James

function predictorCalc()  //Predicting future race results
{

var d1 = document.predictor.d1.value;
var t1 = document.predictor.time1hr.value * 60 * 60 + document.predictor.time1min.value * 60 + document.predictor.time1sec.value * 1;
var deg = document.predictor.deg.value;
var d2 = document.predictor.d2.value;   
  
//NEED TO INPUT IF & ELSE IF STATEMENTS HERE TO MANIPULATE THE RESULT DEPENDING ON WHETHER THE DISTANCE SELECTED IN D1 or D2 is KM OR MILES//

t2 = t1 * (Math.pow((d2 / d1), deg)); //predicted time in seconds (Equation T2=T1*(D2/D1)1.06)

phr = Math.floor(t2/3600);                                               //total hrs
pmin = Math.floor((t2 - (phr*(3600)))/60);               //total mins
psec = Math.floor(t2 - (phr*(3600))-(pmin*60));  //total secs

document.predictor.time2hr.value = round(phr,0);
document.predictor.time2min.value = round(pmin,0);
document.predictor.time2sec.value = round(psec,0);

}


function round(x) {
  return Math.round(x*100)/100;
}
<form name="predictor">

<table>
<tbody>
        <tr>
                <td>D1 (Original distance)</td>
                <td><input type="text" name="d1" size="3">km</td>
                <td>
                        <select size="1" name="d1units">
                        <option selected="" value="k">Kilometers</option>
                        <option value="m">Miles</option>
                        </select>
                </td>
        </tr>
                        
        <tr>
                <td>T1 (original time)</td>
                <td><input name="time1hr" size="3" maxlength="2" type="text">hr</td>
                <td><input type="text" name="time1min" size="3">min</td>
                <td><input type="text" name="time1sec" size="3">sec</td>
        </tr>
                        
        <tr>
                <td>D2 (Future distance competing in)</td>
                <td><input type="text" name="d2" size="3">km</td>
                <td>
          <select size="1" name="d2units">
                                <option selected="" value="k">Kilometers</option>
                                <option value="m">Miles</option>
                  </select>
                </td>
        </tr>
        
    <tr>
                <td>Performance Degradation</td>
                <td colspan="3"><input name="deg" size="2" maxlength="4" type="text" value="1.06">(6% or 1.06 as standard)</td>
        </tr>
  
        <tr>
                <td><input onclick="predictorCalc()" value="Calculate" type="button"></td>
        </tr>
                        
        <tr>
                <td>T2 (Predicted future time)</td>
                <td><input name="time2hr" size="3" maxlength="2" type="text">hr</td>
                <td><input type="text" name="time2min" size="3">min</td>
                <td><input type="text" name="time2sec" size="3">sec</td>
        </tr>
                
</tbody>
</table>
</form>

Aucun commentaire:

Enregistrer un commentaire