Quotes help make search much faster. Example: "Practice Makes Perfect"

Sunday, April 28, 2013

Array Element Tally


Say you had an election, and you needed to tally the votes for each candidate. All you need to do is enter the name of each candidate once it is read. Then, click the ADD button (or press the ENTER key) to add it to the array, as an array element. If you made a mistake, just click the REMOVE button to remove the last element added. The tally results are updated automatically. Each name will show the number of votes it received as well as the total votes cast for the entire election.

Type in the ARRAY ELEMENT:

Add

Remove

TALLY:

Saturday, April 27, 2013

Codecademy Sample Solution: Challenge Time


<!DOCTYPE html>
<html>
    <head>
 <title> Challenge Time! </title>
      <link type='text/css' rel='stylesheet' href='style.css'/>
</head>
<body>
      <p>
        <?php
          // Your code here
            class Cat{
                public $isAlive = true;
                public $numLegs = 4;
                public $name;
               
                public function __construct($name){
                    $this->name = $name;
                }
               
                public function meow(){
                    return "Meow meow";
                }
            }
           
            $cat1 = new Cat('CodeCat');
            echo $cat1->meow();
        ?>
      </p>
    </body>
</html>

Codecademy Sample Solution: Putting It All Together, Part II


<!DOCTYPE html>
<html>
<head>
 <title> Practice makes perfect! </title>
      <link type='text/css' rel='stylesheet' href='style.css'/>
</head>
<body>
      <p>
        <!-- Your code here -->
        <?php
            class Dog{
                public $numLegs = 4;
                public $name;
               
                public function __construct($name){
                    $this->name = $name;
                }
               
                public function bark(){
                    return "Woof!";
                }
               
                public function greet(){
                    return "My name is $this->name.";
                }
            }
           
            $dog1 = new Dog('Barker');
            $dog2 = new Dog('Amigo');
            echo $dog1->bark();
            echo $dog2->greet();
        ?>
      </p>
    </body>
</html>

Codecademy Sample Solution: Putting It All Together, Part I


<!DOCTYPE html>
<html>
<head>
 <title> Practice makes perfect! </title>
      <link type='text/css' rel='stylesheet' href='style.css'/>
</head>
<body>
      <p>
        <!-- Your code here -->
        <?php
            class Dog{
                public $numLegs = 4;
                public $name;
               
                public function __construct($name){
                    $this->name = $name;
                }
               
                public function bark(){
                   
                }
               
                public function greet(){
                   
                }
            }
        ?>
      </p>
    </body>
</html>

Codecademy Sample Solution: A Method to the Madness


<!DOCTYPE html>
<html>
<head>
 <title>Reconstructing the Person Class</title>
      <link type='text/css' rel='stylesheet' href='style.css'/>
</head>
<body>
      <p>
        <!-- Your code here -->
        <?php
            class Person{
                public $isAlive = true;
                public $firstname;
                public $lastname;
                public $age;
               
                public function __construct($firstname, $lastname, $age){
                    $this->firstname = $firstname;
                    $this->lastname = $lastname;
                    $this->age = $age;
                }
               
                public function greet(){
                    return "Hello, my name is " . $this->firstname . " " . $this->lastname . ". Nice to meet you! :-)";
                }
            }
            $teacher = new Person('boring', '12345', 12345);
            echo $teacher->greet();
            $student = new Person('Anony', 'mous', 100);
            echo $student->greet();
        ?>
      </p>
    </body>
</html>

Codecademy Sample Solution: Property Panic (2)


<!DOCTYPE html>
<html>
<head>
 <title>Reconstructing the Person Class</title>
      <link type='text/css' rel='stylesheet' href='style.css'/>
</head>
<body>
      <p>
        <!-- Your code here -->
        <?php
            class Person{
                public $isAlive = true;
                public $firstname;
                public $lastname;
                public $age;
               
                public function __construct($firstname, $lastname, $age){
                    $this->firstname = $firstname;
                    $this->lastname = $lastname;
                    $this->age = $age;
                }
            }
            $teacher = new Person('boring', '12345', 12345);
            $student = new Person('Anony', 'mous', 100);
            echo $student->age;
        ?>
      </p>
    </body>
</html>

Codecademy Sample Solution: Property Panic (1)


<!DOCTYPE html>
<html>
<head>
 <title>Reconstructing the Person Class</title>
      <link type='text/css' rel='stylesheet' href='style.css'/>
</head>
<body>
      <p>
        <!-- Your code here -->
        <?php
            class Person{
                public $isAlive = true;
                public $firstname;
                public $lastname;
                public $age;
            }
            $teacher = new Person();
            echo "$teacher->isAlive";
            $student = new Person();
        ?>
      </p>
    </body>
</html>

Codecademy Sample Solution: Building Your First Class


<!DOCTYPE html>
<html>
<head>
 <title>Reconstructing the Person Class</title>
      <link type='text/css' rel='stylesheet' href='style.css'/>
</head>
<body>
      <p>
        <!-- Your code here -->
        <?php
            class Person{
               
            }
            $teacher = new Person();
            $student = new Person();
        ?>
      </p>
    </body>
</html>

Friday, April 26, 2013

Codecademy Sample Solution: Putting it All Together


<html>
  <head>
    <title>I am the King of Arrays!</title>
  </head>
  <body>
    <p>
      <?php
      // On the line below, create your own associative array:
      $myArray = array('feet'=>'shoes', 'hands'=>'gloves', 'head'=>'hat');

      // On the line below, output one of the values to the page:
      print $myArray['head']."<br/>";
         
      // On the line below, loop through the array and output
      // *all* of the values to the page:
      foreach($myArray as $key => $value){
          print $key .'=&gt;'. $value."<br/>";
      }
   
      ?>
    </p>
  </body>
</html>

Codecademy Sample Solution: Multidimensional Arrays


<html>
  <head>
    <title>Blackjack!</title>
  </head>
  <body>
    <p>
      <?php
        $deck = array(array('2 of Diamonds', 2),
                      array('5 of Diamonds', 5),
                      array('7 of Diamonds', 7),
                      array('8 of Clubs', 8));
       
      // Imagine the first chosen card was the 7 of Diamonds.
      // This is how we would show the user what they have:
      echo 'You have the ' . $deck[3][0] . '!';
      ?>
    </p>
  </body>
</html>

Codecademy Sample Solution: Iterating Over Associative Arrays


<html>
  <head>
    <title>Iteration Nation</title>
  </head>
  <body>
    <p>
      <?php  
        $food = array('pizza', 'salad', 'burger');
        $salad = array('lettuce' => 'with',
                   'tomato' => 'without',
                   'onions' => 'with');
   
      // Looping through an array using "for".
      // First, let's get the length of the array!
      $length = count($food);
   
      // Remember, arrays in PHP are zero-based:
      for ($i = 0; $i < $length; $i++) {
        echo $food[$i] . '<br />';
      }
   
      echo '<br /><br />I want my salad:<br />';
   
      // Loop through an associative array using "foreach":
      foreach ($salad as $ingredient=>$include) {
        echo $include . ' ' . $ingredient . '<br />';
      }
   
      echo '<br /><br />';
   
      // Create your own array here and loop
      // through it using foreach!
      $gadget = array('programming' => 'iMac', 'calls' => 'iPhone', 'ebooks' => 'iPad');
      foreach ($gadget as $key => $value){
        echo "I use the ".$value." for ".$key.".<br/>";
      }

      ?>
    </p>
  </body>
</html>

Codecademy Sample Solution: Accessing Associative Arrays


<html>
  <head>
    <title>Accessing Associative Arrays</title>
  </head>
  <body>
    <p>
      <?php
        // This is an array using integers as the indices...
        $myArray = array(2012, 'blue', 5, 'BMW');

        // ...and this is an associative array:
        $myAssocArray = array('year' => 2012,
                        'colour' => 'blue',
                        'doors' => 5,
                        'make' => 'BMW');
           
        // This code will output "blue".
        echo $myArray[1];
        echo '<br />';
           
        // Add your code here!
        echo "My car was purchased in $myArray[0] and has ". $myAssocArray['doors'] ." doors.";
      ?>
    </p>
  </body>
</html>

Codecademy Sample Solution: Using Arrays as Maps


<html>
  <head>
    <title>Making the Connection</title>
  </head>
  <body>
    <p>
      <?php
        // This is an array using integers as the indices.
        // Add 'BMW' as the last element in the array!
        $car = array(2012, 'blue', 5, 'BMW');

        // This is an associative array.
        // Add the make => 'BMW' key/value pair!
        $assocCar = array('year' => 2012,
                   'colour' => 'blue',
                   'doors' => 5,
                   'make' => 'BMW');
           
        // This code should output "BMW"...
        echo $car[3];
        echo '<br />';
           
        // ...and so should this!
        echo $assocCar['make'];
      ?>
    </p>
  </body>
</html>

Codecademy Sample Solution: Review of Arrays


<html>
  <head>
    <title>Array Review</title>
  </head>
  <body>
    <p>
      <?php
        $fruits = array("bananas", "apples", "pears");              /* Your code here! */
        echo 'I love eating ' . $fruits[1]/* Your code here! */ . ' too!';
      ?>
    </p>
  </body>
</html>

Codecademy Sample Solution: Practice Makes Perfect


<html>
<head>
<title></title>
</head>
<body>
      <p>
        <?php
            function aboutMe($name, $age){
                echo "Hello! My name is $name, and I am $age years old.";
            }
           
            aboutMe("Anonymous",100);
        ?>
      </p>
    </body>
</html>

Codecademy Sample Solution: Parameters and Arguments


<html>
<head>
<title></title>
</head>
<body>
      <p>
        <?php
            function greetings($name){
                echo "Greetings, $name!";
            }
           
            greetings("Anonymous");
        ?>
      </p>
    </body>
</html>

Codecademy Sample Solution: The Return Keyword


<html>
<head>
<title></title>
</head>
<body>
      <?php
           
        function returnName(){
            return "Anonymous";
        }
       
        print returnName();
      ?>
    </body>
</html>

Codecademy Sample Solution: Calling Your Function


<html>
<head>
<title></title>
</head>
<body>
      <p>
        <?php
        // Write your function below!
       
        function displayName(){
            echo "Anonymous";
        }
       
        displayName();
        ?>
      </p>
    </body>
</html>

Codecademy Sample Solution: Your First Function


<html>
<head>
<title></title>
</head>
<body>
      <p>
        <?php
        // Write your function below!
       
        function displayName(){
            echo "Anonymous";
        }
       
        displayName();
        ?>
      </p>
    </body>
</html>

Codecademy Sample Solution: Function Refresher


<html>
    <head>
<title></title>
</head>
<body>
      <p>
        <?php
            echo strlen("Anonymous");
        ?>
      </p>
    </body>
</html>

Thursday, April 25, 2013

Codecademy Sample Solution: Show What You Know!


<html>
    <p>
<?php
// Create an array and push on the names
    // of your closest family and friends
    $somearray = array();
    array_push($somearray, "Garfield");
    array_push($somearray, "Odie");
    array_push($somearray, "John");
// Sort the list
    sort($somearray);
// Randomly select a winner!
    $arraylength = count($somearray);
    $winner = $somearray[rand(0,$arraylength-1)];
// Print the winner's name in ALL CAPS
    print strtoupper($winner);
?>
</p>
</html>

Codecademy Sample Solution: Array Functions II


<html>
    <p>
<?php
// Create an array with several elements in it,
// then sort it and print the joined elements to the screen
    $somearray = array(1,3,4,6,5,7,2);
    sort($somearray);
    print join(", ", $somearray);
?>
</p>
<p>
<?php
// Reverse sort your array and print the joined elements to the screen
    rsort($somearray);
    print join(", ", $somearray);
?>
</p>
</html>

Codecademy Sample Solution: Array Functions I


<html>
    <p>
<?php
// Create an array and push 5 elements on to it, then
    // print the number of elements in your array to the screen
    $fruitbasket = array();
    array_push($fruitbasket, "apple");
    array_push($fruitbasket, "banana");
    array_push($fruitbasket, "grape");
    array_push($fruitbasket, "mango");
    array_push($fruitbasket, "orange");
    print count($fruitbasket);
?>
</p>
</html>

Codecademy Sample Solution: Math Functions I


<html>
    <p>
    <?php
    // Try rounding a floating point number to an integer
    // and print it to the screen
    print round(M_PI);
    ?>
    </p>
    <p>
    <?php
    // Try rounding a floating point number to 3 decimal places
    // and print it to the screen
    print round(M_PI,3);
    ?>
    </p>
</html>

Codecademy Sample Solution: String Functions II


<html>
    <p>
    <?php
    // Print out the position of a letter that is in
    // your own name
    $name = "Anonymous";
    print strpos($name, "o");
    ?>
    </p>
    <p>
    <?php
    // Check for a false value of a letter that is not
    // in your own name and print out an error message
    if(strpos($name, "z")===false){
        print "The letter 'z' was not found in $name.";
    }
    ?>
    </p>
</html>

Codecademy Sample Solution: String Functions I


<html>
  <p>
    <?php
    // Get a partial string from within your own name
    // and print it to the screen!
    $name = "Anonymous";
    print substr($name,3,3);
    ?>
  </p>
  <p>
    <?php
    // Make your name upper case and print it to the screen:
    print strtoupper($name);
    ?>
  </p>
  <p>
    <?php
    // Make your name lower case and print it to the screen:
    print strtolower($name);
    ?>
  </p>
</html>

Codecademy Sample Solution: Introducing Functions


<html>
  <p>
    <?php
    // Get the length of your own name
    // and print it to the screen!
    $namelength = strlen("Anonymous");
    print "<p>$namelength</p>";
    ?>
  </p>
</html>

Tuesday, April 23, 2013

Codecademy Sample Solution: All On Your Own!


<!DOCTYPE html>
<html>
    <head>
<title>Your own do-while</title>
        <link type='text/css' rel='stylesheet' href='style.css'/>
</head>
<body>
    <?php
        //write your do-while loop below
        $loopCount = 0;
        do{
            echo "<p>LoopCount value is {$loopCount}.</p>";
            $increment = rand(0,2);
            $loopCount = $loopCount + $increment;
        }while($loopCount<15);
    ?>
    </body>
</html>

Codecademy Sample Solution: Completing the Loop


<!DOCTYPE html>
<html>
    <head>
<title>Much a do-while</title>
</head>
<body>
    <?php
$loopCond = false;
do{
echo "<p>The loop ran even though the loop condition is false.</p>";
}while($loopCond);
echo "<p>Now the loop is done running.</p>";
    ?>
    </body>
</html>

Codecademy Sample Solution: Using Endwhile


<!DOCTYPE html>
<html>
    <head>
<title>A loop of your own</title>
        <link type='text/css' rel='stylesheet' href='style.css'/>
</head>
<body>
    <?php
//Add while loop below
        $loopCount = 0;
        while($loopCount < 10):
            echo "<p>The current loop count is $loopCount</p>";
            $loopCount++;
        endwhile;
    ?>
    </body>
</html>

Codecademy Sample Solution: Your First While Loop


<!DOCTYPE html>
<html>
    <head>
<title>A loop of your own</title>
        <link type='text/css' rel='stylesheet' href='style.css'/>
</head>
<body>
    <?php
//Add while loop below
        $loopCount = 0;
        while($loopCount < 10){
            echo "<p>The current loop count is $loopCount</p>";
            $loopCount++;
        }
    ?>
    </body>
</html>

Codecademy Sample Solution: While Loop Syntax


<!DOCTYPE html>
<html>
    <head>
<title>Your First PHP while loop!</title>
</head>
<body>
    <?php
$loopCond = true;
while ($loopCond){
//Echo your message that the loop is running below
echo "<p>The loop is running.</p>";
$loopCond = false;
}
echo "<p>And now it's done.</p>";
    ?>
    </body>
</html>

Sunday, April 21, 2013

Codecademy Sample Solution: All On Your Own


<html>
  <head>
    <title></title>
  </head>
  <body>
    <p>
      <?php
        $yardlines = array("The 50... ", "the 40... ",
        "the 30... ", "the 20... ", "the 10... ");
        // Write your foreach loop below this line
        foreach($yardlines as $yardline){
            echo "$yardline";
        }
       
        // Write your foreach loop above this line
        echo "touchdown!";
      ?>
    </p>
  </body>
</html>

Codecademy Sample Solution: Practicing With ForEach


<html>
  <head>
    <title></title>
  </head>
  <body>
    <p>
      <?php
        $sentence = array("I'm ", "learning ", "PHP!");
       
        foreach ($sentence as $word) {
          echo $word;
        }
      ?>
    </p>
  </body>
</html>

Codecademy Sample Solution: When To Use 'For'


<html>
  <head>
    <title>Solo For Loop!</title>
  </head>
  <body>
    <p>
      <?php
      // Write your for loop below!
      for( $i = 50; $i > 4; $i = $i - 5){
        echo "<p>$i</p>";
      }
      ?>
    </p>
  </body>
</html>

Codecademy Sample Solution: Writing Your First 'For' Loop


<html>
  <head>
    <title>Solo For Loop!</title>
  </head>
  <body>
    <p>
      <?php
      // Write your for loop below!
      for( $i = 10; $i < 101; $i = $i + 10){
        echo "<p>$i</p>";
      }
      ?>
    </p>
  </body>
</html>

Codecademy Sample Solution: 'For' Loop Syntax


<html>
  <head>
    <title>For Loops</title>
  </head>
  <body>
    <p>
      <?php
        // Echoes the first five even numbers
        for ($i = 2; $i < 11; $i = $i + 2) {
          echo $i;
        }
      ?>
    </p>
  </body>
</html>

Saturday, April 20, 2013

Codecademy Sample Solution: Deleting Array Elements


<html>
  <head>
    <title>Modifying Elements</title>
  </head>
  <body>
    <p>
      <?php
        $languages = array("HTML/CSS",
        "JavaScript", "PHP", "Python", "Ruby");
        // Write the code to remove Python here!
       
        unset($languages[3]);
       
        // Write your code above this line. Don't
        // worry about the code below just yet; we're
        // using it to print the new array out for you!
       
        foreach($languages as $lang) {
          print "<p>$lang</p>";
        }
      ?>
    </p>
  </body>
</html>

Codecademy Sample Solution: Modifying Array Elements


<html>
  <head>
    <title>Modifying Elements</title>
  </head>
  <body>
    <p>
      <?php
        $languages = array("HTML/CSS",
        "JavaScript", "PHP", "Python", "Ruby");
       
        // Write the code to modify
        // the $languages array!
        $languages[2] = "C++";
        echo $languages[2];
      ?>
    </p>
  </body>
</html>

Codecademy Sample Solution: Access by Offset with {}


<html>
  <head>
    <title>Accessing Elements</title>
  </head>
  <body>
    <p>
      <?php
        $tens = array(10, 20, 30, 40, 50);
        echo $tens{2};
      ?>
    </p>
  </body>
</html>

Codecademy Sample Solution: Access by Offset with []


<html>
  <head>
    <title>Accessing Elements</title>
  </head>
  <body>
    <p>
      <?php
        $tens = array(10, 20, 30, 40, 50);
        echo $tens[2];
      ?>
    </p>
  </body>
</html>

Codecademy Sample Solution: Creating Your First Array


<html>
  <head>
    <title>My First Array</title>
  </head>
  <body>
    <p>
      <?php
        $friends = array("Bosh", "Lebron", "Wade");
      ?>
    </p>
  </body>
</html>

Codecademy Sample Solution: Array Syntax


<html>
  <head>
    <title>Woot, More Arrays!</title>
  </head>
  <body>
    <p>
      <?php
        // Add your array elements after
        // "Beans" and before the final ")"
        $array = array("Egg", "Tomato", "Beans", "Chips", "Sausage" );
      ?>
    </p>
  </body>
</html>

Thursday, April 18, 2013

Codecademy Sample Solution: All On Your Own!


<!DOCTYPE html>
<html>
    <head>
<title></title>
</head>
<body>
    <?php
        $GIjoe = 'Snake Eyes';
        switch($GIjoe):
            case 'Lady Jane':
                echo 'Baroness is hotter';
                break;
            case 'Duke':
                echo 'Dead in thirty minutes';
                break;
            case 'Snake Eyes':
                echo 'Silence is deadly';
                break;
            default:
                echo 'Bruce ain\'t no Joe';
        endswitch;
    ?>
</body>
</html>

Codecademy Sample Solution: Using "Endswitch". Syntactic Sugar!


<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
    <?php
    $i = 5;
   
    switch ($i):
        case 0:
            echo '$i is 0.';
            break;
        case 1:
        case 2:
        case 3:
        case 4:
        case 5:
            echo '$i is somewhere between 1 and 5.';
            break;
        case 6:
        case 7:
            echo '$1 is either 6 or 7.';
            break;
        default:
            echo "I don't know how much \$i is";
            break;
    endswitch;
    ?>
    </body>
</html>

Codecademy Sample Solution: Multiple Cases: Falling Through!


<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
    <?php
    $i = 5;
   
    switch ($i) {
        case 0:
            echo '$i is 0.';
            break;
        case 1:
        case 2:
        case 3:
        case 4:
        case 5:
            echo '$i is somewhere between 1 and 5.';
            break;
        case 6:
        case 7:
            echo '$1 is either 6 or 7.';
            break;
        default:
            echo "I don't know how much \$i is";
            break;
    }
    ?>
    </body>
</html>

Codecademy Sample Solution: Switch Syntax


<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
    <?php
    $fruit = "Apple";
   
    switch ($fruit) {
        case 'Apple':
            echo "Yummy.";
            break;
        default:
            echo "Mmm. Tasty.";
    }
   
    ?>
    </body>
</html>

Codecademy Sample Solution: Glance at the Past!


<!DOCTYPE html>
<html>
    <head>
<title></title>
</head>
<body>
    <?php
        $var = 1;
        if($var < 1){
            echo "Variable is less than 1.";
        }
        elseif($var > 1){
            echo "Variable is greater than 1.";
        }
        else{
            echo "Variable is less than 1.";
        }
    ?>
    </body>
</html>

Monday, April 15, 2013

Codecademy Sample Solution: All Together Now!


<html>
  <head>
    <title>If, Elseif, and Else</title>
  </head>
  <body>
    <p>
      <?php
        $guess = 7;
        $number = 7;
       
        // Write your if/elseif/else statement here!
        if($guess<$number){
            echo "Too low!";
        }
        elseif($guess>$number){
            echo "Too high!";
        }
        else{
            echo "You win!";
        }
      ?>
    </p>
  </body>
</html>

Codecademy Sample Solution: Else + If = Elseif


<html>
  <head>
    <title>Our Shop</title>
  </head>
  <body>
    <p>
      <?php
        $items = 1;    // Set this to a number greater than 5 to graduate from the exercise
        if($items == 1){
          echo "Sorry, no discount!";    
        }
        elseif ($items > 5){
          echo "You get a 10% discount!";
        }
        else{
          echo "You get a 5% discount!";  
        }
      ?>
    </p>
  </body>
</html>

Codecademy Sample Solution: Adding an Else


<html>
  <head>
    <title>Our Shop</title>
  </head>
  <body>
    <p>
      <?php
        $items = 4;    // This might cause an error since the exercise still expects a "TRUE" answer
        if ($items > 5){
          echo "You get a 10% discount!";
        }
        else{
          echo "You get a 5% discount!";  
        }
      ?>
    </p>
  </body>
</html>

Codecademy Sample Solution: Your First 'if'


<html>
  <head>
    <title>Our Shop</title>
  </head>
  <body>
    <p>
      <?php
        $items = 7;    // Set this to a number greater than 5!
        if ($items > 5){
          echo "You get a 10% discount!";
        }
      ?>
    </p>
  </body>
</html>

Codecademy Sample Solution: Making Comparisons


<html>
  <head>
    <title>Comparing Numbers</title>
  </head>
  <body>
    <p>
      <?php
        $teabags = 3;
        if($teabags < 7)
            echo "true";
      ?>
    </p>
  </body>
</html>

This is an example of scrolling text using Javascript.

Popular Posts