Web Develop Forum: Lesson 4 - Homework. - Web Develop Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Lesson 4 - Homework. Getting ready for some real world applications

#1 User is offline   Jack Icon

  • Member
  • PipPip
  • Group: Teacher
  • Posts: 25
  • Joined: 31-January 09
  • Location:Australia

Post icon  Posted 25 February 2009 - 10:43 AM

Hi,

Ok - instead of having the homework specific to this week's work - here are some things you'll need to know that encompass everything we've done so far.

1) Write a isPrime() function, that accepts a number, and returns a boolean - based on testing if it really is a prime number
2) Find a way to calculate 10% tax on any given value, and allow for $x amount of "tax free" things. I'll leave this up to you on how you want to structure it ( as a function or not etc).

3) Look into recursive functions ( functions that call themselves ) - and suggest one web-related reason we might use it ( I'll be using this in a few weeks when we cover OOP).

4) Later on we'll look at exceptions. As a primer for that, write a user friendly function you could do if you found an error ( so it will need to display the error, and data about the error).


That's it. If everyone can post in the main/original thread on what feature you want to program first, that will help. At the moment I'm looking at a simple login form - with or without captcha I'm not sure.

C ya
0

#2 User is offline   Jack Icon

  • Member
  • PipPip
  • Group: Teacher
  • Posts: 25
  • Joined: 31-January 09
  • Location:Australia

Posted 04 March 2009 - 12:44 PM

Answers:

1)

CODE
function isPrime($n) {
    $i=2;
    while ( $i <= $n/2) {
        if( ($n % $i) == 0) {
            return False;
        }
        $i++;
    }
    return True;
}

echo var_dump(isPrime(13))."<br>";
echo var_dump(isPrime(4))."<br>";
echo var_dump(isPrime(21))."<br>";
echo var_dump(isPrime(19))."<br>";


2)
CODE
function addTax($val, $free=0) {
    return ($addTax-$free)*1.1;
}


3) Recursive functions are ones that call themselves. As a result, they pass teh same arguments - and might be used for navigating a tree: "If next node is a branch, follow that branch" sort of logic.


4)

function displayError($msg) {
echo "An error was found, please try again later.";
die(); //Yes I know the message could have gone in here
}
// ^^^ This code stops straight - proper errors are caught and the script continues.



Well thats it!

0

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users