<?php

/**
 * @preserve 9e3f9013ed1e2f0236bfd253cace3df9
 *
 * 9e3f9013ed1e2f0236bfd253cace3df9
 * htmlphp
 * lab3
 * v2
 * rasb14
 * 2017-09-12 07:09:25
 * v2.3.3 (2017-06-12)
 *
 * Generated 2017-09-12 09:09:26 by dbwebb lab-utility v2.3.3 (2017-06-12).
 * https://github.com/mosbth/lab
 */

// Set error reporting to verbose
error_reporting(-1);              // Report all type of errors
ini_set('display_errors', 1);     // Display all errors

// Load and create object with lab utilities
require __DIR__ . "/.Dbwebb.php";
$dbwebb = new Dbwebb();



/** ===================================================================
 * Lab 3 - Htmlphp
 *
 * If you need to peek at examples or just want to know more, take a look at
 * the [PHP Manual](http://php.net/manual/en/langref.php).
 *
 * There you will find everything this lab will go through and much more.
 *
 */



/** -------------------------------------------------------------------
 * Section 1 . Arrays - with numeric index
 *
 *
 *
 */



/**
 * Exercise 1.1 (1 points)
 *
 * Create an array, called `countries` with the items: `[United Kingdom,
 * Bahamas, Ukraine]`.
 *
 * Answer with the second item in the array.
 *
 * Write your code below and put the answer into the variable ANSWER.
 */

$countries = ["United Kingdom", "Bahamas", "Ukraine"];

$ANSWER = $countries[1];

// I will now test your answer - change false to true to get a hint.
echo $dbwebb->assertEqual("1.1", $ANSWER, false);

/**
 * Exercise 1.2 (1 points)
 *
 * Create a new array called `capitals` with the items: `[London, Nassau,
 * Kiev]`.
 *
 * Answer with a string containing each country from the `countries`-array
 * followed by the corresponding capital. Use the format `"country = capital,
 * country = capital..."`
 *
 * Write your code below and put the answer into the variable ANSWER.
 */

$capitals = ["London", "Nassau", "Kiev"];
$countrycapitals = "";
for ($i = 0; $i <= 2; $i++) {
    if ($i == 2) {
        $countrycapitals .= $countries[$i] . " = " . $capitals[$i];
    } else {
        $countrycapitals .= $countries[$i] . " = " . $capitals[$i] . ", ";
    }
}

$ANSWER = $countrycapitals;

// I will now test your answer - change false to true to get a hint.
echo $dbwebb->assertEqual("1.2", $ANSWER, false);

/**
 * Exercise 1.3 (1 points)
 *
 * Create an array, called `numbers1` with the items: `[522, 87, 54.98, 3,
 * 6533]`. Answer with the sum of the first two items.
 *
 * Write your code below and put the answer into the variable ANSWER.
 */

$numbers1 = [522, 87, 54.98, 3, 6533];

$ANSWER = $numbers1[0] + $numbers1[1];

// I will now test your answer - change false to true to get a hint.
echo $dbwebb->assertEqual("1.3", $ANSWER, false);

/**
 * Exercise 1.4 (1 points)
 *
 * Use your arrays `numbers1` and `capitals` to change item at index 4 in
 * `numbers1` to the item at index 2 in `capitals`. Answer with the array
 * `numbers1`.
 *
 * Write your code below and put the answer into the variable ANSWER.
 */

$numbers1[4] = $capitals[2];

$ANSWER = $numbers1;

// I will now test your answer - change false to true to get a hint.
echo $dbwebb->assertEqual("1.4", $ANSWER, false);

/**
 * Exercise 1.5 (1 points)
 *
 * Use your array `countries` and concatenate the first and the last item as a
 * string. Separate the items with a hyphen `-` and answer with the result.
 *
 * Write your code below and put the answer into the variable ANSWER.
 */

$ANSWER = $countries[0] . "-" . $countries[2];

// I will now test your answer - change false to true to get a hint.
echo $dbwebb->assertEqual("1.5", $ANSWER, false);

/** -------------------------------------------------------------------
 * Section 2 . Arrays - with keys
 *
 *
 *
 */



/**
 * Exercise 2.1 (1 points)
 *
 * Use your `countries` and `capitals` arrays and create another array called
 * `keyArray`. The country should be the key and the capital should be the
 * value. Answer with the new array. (hint: `"country" => "capital"`)
 *
 * Write your code below and put the answer into the variable ANSWER.
 */

$keyArray = array(
    $countries[0] => $capitals[0],
    $countries[1] => $capitals[1],
    $countries[2] => $capitals[2]
);

$ANSWER = $keyArray;

// I will now test your answer - change false to true to get a hint.
echo $dbwebb->assertEqual("2.1", $ANSWER, false);

/**
 * Exercise 2.2 (1 points)
 *
 * Add the key `"Canada"` with the value `"Ottawa"` to your `keyArray`. Answer
 * with the updated array.
 *
 * Write your code below and put the answer into the variable ANSWER.
 */

$keyArray += array("Canada" => "Ottawa");

$ANSWER = $keyArray;

// I will now test your answer - change false to true to get a hint.
echo $dbwebb->assertEqual("2.2", $ANSWER, false);

/**
 * Exercise 2.3 (1 points)
 *
 * Answer with the value for the key `"Bahamas"`.
 *
 * Write your code below and put the answer into the variable ANSWER.
 */

$ANSWER = $keyArray["Bahamas"];

// I will now test your answer - change false to true to get a hint.
echo $dbwebb->assertEqual("2.3", $ANSWER, false);

/** -------------------------------------------------------------------
 * Section 3 . Arrays - built-in functions
 *
 *
 *
 */



/**
 * Exercise 3.1 (1 points)
 *
 * Find the number of items in the array `[324, 36, 20.02, 8, 4998]`. Answer
 * with the result as an integer.
 *
 * Write your code below and put the answer into the variable ANSWER.
 */

$numberArray = [324, 36, 20.02, 8, 4998];

$ANSWER = count($numberArray);

// I will now test your answer - change false to true to get a hint.
echo $dbwebb->assertEqual("3.1", $ANSWER, false);

/**
 * Exercise 3.2 (1 points)
 *
 * Sort the array `[324, 36, 20.02, 8, 4998]` in decending order. Make sure
 * that it does maintain the key association. Answer with the sorted array.
 *
 * Write your code below and put the answer into the variable ANSWER.
 */

$sortedArray = $numberArray;
arsort($sortedArray);

$ANSWER = $sortedArray;

// I will now test your answer - change false to true to get a hint.
echo $dbwebb->assertEqual("3.2", $ANSWER, false);

/**
 * Exercise 3.3 (1 points)
 *
 * Use `pop()` on the array `[324, 36, 20.02, 8, 4998]` and answer with the
 * popped item.
 *
 * Write your code below and put the answer into the variable ANSWER.
 */

$popArray = $numberArray;

$ANSWER = array_pop($popArray);

// I will now test your answer - change false to true to get a hint.
echo $dbwebb->assertEqual("3.3", $ANSWER, false);

/**
 * Exercise 3.4 (1 points)
 *
 * Use `push()` on the array `[285, 11, 9.75, 9, 2216]` and insert the number
 * 56. Answer with the resulting array.
 *
 * Write your code below and put the answer into the variable ANSWER.
 */

$numberArray2 = [285, 11, 9.75, 9, 2216];
array_push($numberArray2, 56);

$ANSWER = $numberArray2;

// I will now test your answer - change false to true to get a hint.
echo $dbwebb->assertEqual("3.4", $ANSWER, false);

/**
 * Exercise 3.5 (1 points)
 *
 * Copy your array `countries` to a new array called `newArray`. Use `shift()`
 * on the new array and answer with the shifted item.
 *
 * Write your code below and put the answer into the variable ANSWER.
 */

$newArray = $countries;

$ANSWER = array_shift($newArray);

// I will now test your answer - change false to true to get a hint.
echo $dbwebb->assertEqual("3.5", $ANSWER, false);

/**
 * Exercise 3.6 (1 points)
 *
 * Use `unshift()` on your `newArray` add the items `"Montana"` and `"Oregon"`
 * in the beginning of the array. Answer with the modified array.
 *
 * Write your code below and put the answer into the variable ANSWER.
 */

array_unshift($newArray, "Montana", "Oregon");

$ANSWER = $newArray;

// I will now test your answer - change false to true to get a hint.
echo $dbwebb->assertEqual("3.6", $ANSWER, false);

/**
 * Exercise 3.7 (1 points)
 *
 * Reverse the order of the array `[324, 36, 20.02, 8, 4998]`. Answer with the
 * modified array.
 *
 * Write your code below and put the answer into the variable ANSWER.
 */

$ANSWER = array_reverse($numberArray);

// I will now test your answer - change false to true to get a hint.
echo $dbwebb->assertEqual("3.7", $ANSWER, false);

/**
 * Exercise 3.8 (1 points)
 *
 * Use `implode()` on your `capital`-array and answer with a string where each
 * item is separated by a hyphen `-`.
 *
 * Write your code below and put the answer into the variable ANSWER.
 */

$text = implode("-", $capitals);

$ANSWER = $text;

// I will now test your answer - change false to true to get a hint.
echo $dbwebb->assertEqual("3.8", $ANSWER, false);

/** -------------------------------------------------------------------
 * Section 4 . Arrays - for-each loop
 *
 *
 *
 */



/**
 * Exercise 4.1 (1 points)
 *
 * Create an array, called `numbers1` with the items: `[21, 5, 4, 6, 76, 2,
 * 18, 85, 55, 1]`. Use a for-each loop to sum each item with 6 and put them
 * in a new array called `summedNumbers1`. Answer with the new array.
 *
 * Write your code below and put the answer into the variable ANSWER.
 */

$numbers1 = [21, 5, 4, 6, 76, 2, 18, 85, 55, 1];
$summedNumbers1 = array();
foreach ($numbers1 as $i) {
    array_push($summedNumbers1, $i + 6);
}

$ANSWER = $summedNumbers1;

// I will now test your answer - change false to true to get a hint.
echo $dbwebb->assertEqual("4.1", $ANSWER, false);

/**
 * Exercise 4.2 (1 points)
 *
 * Create a new array called `numbers2` with the items `[11, 4, 41, 67, 99,
 * 22, 8, 83, 5, 16]`. Use at least a for-each loop to add all numbers larger
 * than 20 to a new array called `largeNumbers`. Answer with the new array.
 *
 * Write your code below and put the answer into the variable ANSWER.
 */

$numbers2 = [11, 4, 41, 67, 99, 22, 8, 83, 5, 16];
$largeNumbers = array();
foreach ($numbers2 as $i) {
    if ($i > 20) {
        array_push($largeNumbers, $i);
    }
}

$ANSWER = $largeNumbers;

// I will now test your answer - change false to true to get a hint.
echo $dbwebb->assertEqual("4.2", $ANSWER, false);

/**
 * Exercise 4.3 (1 points)
 *
 * Create an array with the keys: `"one"`, `"two"`, `"three"`, `"four"` and
 * `"five"` and the values: 1, 2, 3, 4, 5. Use a foreach-loop to add all keys
 * and values to an array in the format: `["key"=value, "key"=value, etc]`.
 * Use `implode()` to make the answer a string with all items separated by a
 * comma `,`.
 *
 * Write your code below and put the answer into the variable ANSWER.
 */

$numbersWKeys = array("one" => 1, "two" => 2, "three" => 3, "four" => 4, "five" => 5);
$textArray = array();
foreach ($numbersWKeys as $key => $value) {
    array_push($textArray, $key . "=" . $value);
}
$text = implode(",", $textArray);

$ANSWER = $text;

// I will now test your answer - change false to true to get a hint.
echo $dbwebb->assertEqual("4.3", $ANSWER, false);

/** -------------------------------------------------------------------
 * Section 5 . Switch-case
 *
 * [PHP Manual about
 * switch](http://se1.php.net/manual/en/control-structures.switch.php)
 *
 */



/**
 * Exercise 5.1 (1 points)
 *
 * Create a switch-case statement that returns the continent a certain country
 * resides in. Use the countries: `"Sweden, Brazil, China, Australia, Canada"`
 * and the continents:
 *     `"Europe, South America, Asia, Oceania, North America"`.
 *
 * Answer with a test on the country: `"Canada"`.
 *
 * Write your code below and put the answer into the variable ANSWER.
 */

$country = "Canada";
$result = "";
switch ($country) {
    case "Sweden":
        $result = "Europe";
        break;
    case "Brazil":
        $result = "South America";
        break;
    case "China":
        $result = "Asia";
        break;
    case "Australia":
        $result = "Oceania";
        break;
    case "Canada":
        $result = "North America";
        break;
    default:
        $result = "Earth";
        break;
}

$ANSWER = $result;

// I will now test your answer - change false to true to get a hint.
echo $dbwebb->assertEqual("5.1", $ANSWER, false);

/** -------------------------------------------------------------------
 * Section 6 . Extra assignments
 *
 * These questions are worth 3 points each. Solve them for extra points.
 *
 */



/**
 * Exercise 6.1 (3 points)
 *
 * A switch-case statement can handle multiple cases with the same result.
 * Create a new switch-case statement that returns the corresponding
 * continent. Use the countries: `"Sweden, Denmark, Finland, Brazil, China,
 * Australia, Canada"` and the continents: `"Europe, South America, Asia,
 * Oceania, North America"`.
 *
 * Answer with a test on the country: `"Denmark"`.
 *
 * Write your code below and put the answer into the variable ANSWER.
 */

$country = "Denmark";
$result = "";
switch ($country) {
    case "Sweden":
    case "Denmark":
    case "Finland":
        $result = "Europe";
        break;
    case "Brazil":
        $result = "South America";
        break;
    case "China":
        $result = "Asia";
        break;
    case "Australia":
        $result = "Oceania";
        break;
    case "Canada":
        $result = "North America";
        break;
    default:
        $result = "Earth";
        break;
}

$ANSWER = $result;

// I will now test your answer - change false to true to get a hint.
echo $dbwebb->assertEqual("6.1", $ANSWER, false);

/**
 * Exercise 6.2 (3 points)
 *
 * Sort the array `[285, 11, 9.75, 9, 2216]` in ascending order. Make sure
 * that it does not maintain the key association. Answer with the sorted
 * array.
 *
 * Write your code below and put the answer into the variable ANSWER.
 */

$toSort = [285, 11, 9.75, 9, 2216];
sort($toSort);

$ANSWER = $toSort;

// I will now test your answer - change false to true to get a hint.
echo $dbwebb->assertEqual("6.2", $ANSWER, false);


// Wrap it up
exit($dbwebb->exitWithSummary());
