Kata: Travel Time and Cost
Kata: Travel time and cost
Here we have the classic problem of calculating the amount of time to travel a given distance at a given speed. We’ll add to the problem by also calculating how much gasoline was used based on a given MPG and how much the gasoline cost, based on a given price per gallon.
Noob: Write, in pseudocode, an algorithm that accepts the following input: distance travelled, speed (rate), MPG, and cost per gallon. The pseudocode should show calculating the amount of time spent traveling and how much the trip cost. Finally, the pseudocode demonstrates outputting what the user entered and the calculated values, formatted to indicate what’s what.
King Code: Complete the Code Monkey and Noob levels. Add to your program some additional “smarts”: The program requests input for all possible data points: distance, speed, time, gas used (in gallons), MPG, price per gallon, and total cost. The program can then calculate the missing values based on what was submitted. For example, given the total cost for the trip and the distance travelled, the program can output the price per gallon. Likewise, given the time and speed, the program can output the distance travelled.
Unicorn: Complete the King Code level. Use form elements with labels to collect the user input. Dynamically calculate and update the values of the text fields as the user enters input. Display suitable error, processing or waiting messages to the user as appropriate while user is entering input. Check user input for validity. Allow user to switch between English and metric units.
Note: you may need the
onclick event handler. Some other event handlers you might find useful for forms are
onchange. Radio buttons can work well for a English/metric toggle.