Jump to content

Generate a Function


nschmutz

Recommended Posts

I need to write a script with the following parameters:

 

-generate a function which takes a number between 1 and 20 (inclusive), and one of square root, square or cube as the operation to be performed on the number. The function should return the square root, square or cube of the given number based on user input.

 

- draw an HTML form to receive from the user the number in a textbox, and the operation to be performed in a drop-down menu

 

- display the result, stating the number entered and the chosen operation (square root, square or cube.) (For example, display "The square of 3 is 9.") Round the result to three decimal places; i.e., there should be at most three digits after the decimal separator period

 

- The textbox and the drop-down should be sticky, and the form should be redrawn when the message is displayed.

 

- If anything other than a number between 1 and 20 is entered (a number less than 1 or greater than 20, or a non-numeric value, etc.) the script should issue a warning, and also redraw the form with the illegitimate value entered.

 

- will have to use "switch" within the function declaration

 

 

 

Appreciate any help you can give.

Link to comment
https://forums.phpfreaks.com/topic/205378-generate-a-function/
Share on other sites

A) What problem or error are you getting with your code that you need help with or what specific programming question do you have?

 

B) We don't do homework, we help with specific coding problems, errors, or questions you have.

 

C) We don't write code based on lists of requirements. There is a freelancing forum section if you want someone to do this for you.

 

 

Link to comment
https://forums.phpfreaks.com/topic/205378-generate-a-function/#findComment-1074818
Share on other sites

if (1 <= $input['number'] && $input['number'] <= 20) // 1 <= x <= 20
{

switch ($input['type']) {
  case 'square_root':
    $output = factory('square_root')->calc($input['number'])->toString(); // 'The square root of $input['number'] is $x'
    break;
  case 'square':
    $output = factory('square')->calc($input['number'])->toString(); // 'The square of $input['number'] is $y'
    break;
  case 'cube':
    $output = factory('cube')->calc($input['number'])->toString(); // 'The cube of $input['number'] is $z'
    break;
}

echo $output;

}

 

Should get you started.

Link to comment
https://forums.phpfreaks.com/topic/205378-generate-a-function/#findComment-1075162
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.