Goat fuel


Cthulhu sponsors goat races every month. The course varies in length, and whether there are hurdles or not. Before each race, Cthulhu feeds the goats, so they have the energy they'll need. Write a program to work out the number of kilocalories needed for a race.

You can try my solution:


The user types in some GET params:

  • goats: The number of goats (must be a number more than zero)
  • course_length: The course length (must be a number of at least 10)
  • hurdles: Whether there are hurdles (must be y or n, allow upper- or lowercase)

Validate the data, showing all appropriate error messages. For example:


Style as shown.

Sample output:


Kilocalories is goats times length times 100 divided by 1,000. If there are hurdles, multiply kilocalories by two.

If more than 100 kilocalories are needed, Cthulhu wants you to bring in Euphonites, his goat nutritionist. Show a message to that effect:

More output

You must use this code, with no changes:

  • <?php
  • // Init error message.
  • $errorMessage = '';
  • // Validate goats.
  • $goats = getParamFromGet('goats');
  • $errorMessage .= checkGoats($goats);
  • // Validate course length.
  • $courseLength = getParamFromGet('course_length');
  • $errorMessage .= checkCourseLength($courseLength);
  • // Validate hurdles.
  • $hurdles = getParamFromGet('hurdles');
  • $errorMessage .= checkHurdles($hurdles);
  • if ($errorMessage == '') {
  •     // All input valid. Compute kilocalories.
  •     $kiloCalories = $goats * $courseLength * 100 / 1000;
  •     // Normalize $hurdles.
  •     $hurdles = strtolower(trim($hurdles));
  •     if ($hurdles == 'y') {
  •         $kiloCalories *= 2;
  •     }
  • }
  • ?><!doctype html>

Put your functions at the bottom of the page, or in a separate file.

Submit your URL, and a zip of all your files. The usual coding standards apply.

Where referenced