<?php

/**
 * @preserve 899c785f4cf095b46cd82a4858586217
 *
 * 899c785f4cf095b46cd82a4858586217
 * htmlphp
 * lab4
 * v2
 * rasb14
 * 2017-10-06 17:12:47
 * v2.3.3 (2017-06-12)
 *
 * Generated 2017-10-06 19:12:47 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 4 - 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 . Basic functions
 *
 *
 *
 */



/**
 * Exercise 1.1 (1 points)
 *
 * Create a function called `sumNumbers()` that should take 2 numbers as
 * arguments and return the sum of them.
 *
 * Answer with a call to the function using the numbers 591 and 119.
 *
 * Write your code below and put the answer into the variable ANSWER.
 */

function sumNumbers($first, $second)
{
    return $first + $second;
}

$ANSWER = sumNumbers(591, 119);

// 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 function called `sumArray()` that should take an array as argument
 * and return the sum of all items in the array.
 *
 * Answer with a call to the function using the array: `[5,946,2,86,9]`.
 *
 * Write your code below and put the answer into the variable ANSWER.
 */

function sumArray($arr)
{
    $result = 0;
    foreach ($arr as $val) {
        $result += $val;
    }
    return $result;
}

$ANSWER = sumArray([5, 946, 2, 86, 9]);

// 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 a function called `modArray()` that should take 2 arguments, an
 * array and a string. Replace the first item in the array with the string and
 * return the array.
 *
 * Answer with a call to the function using the arguments: `[4,256,5,13,1]`
 * and `"onion"`.
 *
 * Write your code below and put the answer into the variable ANSWER.
 */

function modArray($arr, $string)
{
    $arr[0] = $string;
    return $arr;
}

$ANSWER = modArray([4,256,5,13,1], "onion");

// 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)
 *
 * Create a function called `printRange()` that should take 2 numbers as
 * arguments, start and stop. The function should add all even numbers between
 * start and stop (not including) to an array and return it.
 *
 * Answer with a call to the function using the arguments: 20 and 40.
 *
 * Write your code below and put the answer into the variable ANSWER.
 */

function printRange($start, $stop)
{
    $arr = array();
    for ($i = $start + 1; $i < $stop; $i++) {
        if ($i % 2 == 0) {
            array_push($arr, $i);
        }
    }
    return $arr;
}

$ANSWER = printRange(20, 40);

// 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)
 *
 * Create a function called `combineArrays()` that takes two arrays as
 * arguments. The function should combine the arrays to one associative array
 * and return it. The first argument is the key and the second argument is the
 * value.
 *
 * Answer with a call to the function using the arguments:
 * `[green,brown,pink,white,gray,blue]` and `[frog,bear,pig,lion,wolf,whale]`.
 *
 * Write your code below and put the answer into the variable ANSWER.
 */

function combineArrays($keyarr, $valarr)
{
    $arr = array();
    for ($i = 0; $i < count($keyarr); $i++) {
        $arr[$keyarr[$i]] = $valarr[$i];
    }
    return $arr;
}

$ANSWER = combineArrays(["green", "brown", "pink", "white", "gray", "blue"], ["frog", "bear", "pig", "lion", "wolf", "whale"]);

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

/**
 * Exercise 1.6 (1 points)
 *
 * Create a function called `euroToDollar()` that takes one argument, the euro
 * amount to convert to dollars. Count 1 Euro as 1.261215 dollars. Return the
 * dollar amount of 292 Euros.
 *
 * Answer with the result as a double with 4 decimals.
 *
 * Write your code below and put the answer into the variable ANSWER.
 */

function euroToDollar($euro)
{
    $conversionRate = 1.261215;
    return round($conversionRate * $euro, 4);
}

$ANSWER = euroToDollar(292);

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

/**
 * Exercise 1.7 (1 points)
 *
 * Create a function called `inRange()` that takes one argument. The function
 * should return `true` if the argument is higher than 50 and lower than 100.
 * If the number is out of range, the function should return `false`. The
 * return type should be boolean.
 *
 * Use the argument 134 and answer with a call to the function.
 *
 * Write your code below and put the answer into the variable ANSWER.
 */

function inRange($val)
{
    return $val > 50 && $val < 100;
}

$ANSWER = inRange(134);

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

/**
 * Exercise 1.8 (1 points)
 *
 * Create a function called `calculateArea()` that takes one argument, the
 * diameter of a circle. The function should return the area of the circle,
 * with 4 decimals.
 *
 * Answer with the result if the diameter is 9. ( hint: `pi()` )
 *
 * Write your code below and put the answer into the variable ANSWER.
 */

function calculateArea($diameter)
{
    $radian = $diameter / 2;
    $area = pi() * pow($radian, 2);
    return round($area, 4);
}

$ANSWER = calculateArea(9);

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

/**
 * Exercise 1.9 (1 points)
 *
 * Create a function called `fibonacci()`. The function should use the
 * [Fibbonacci Sequence](http://en.wikipedia.org/wiki/Fibonacci_number),
 * starting with 1 and 2. Return the sum of all odd numbers in the sequence,
 * when the sequence value dont exceed 1.000.000.
 *
 * Answer with a call of the function. A Fibonacci-sequence can look like
 * this: 1, 2, 3, 5, 8, 13, 21, 34, 55 etc. You add the current value with the
 * last, i.e. `1+2=3, 3+2=5, 5+3=8 etc`.
 *
 * Write your code below and put the answer into the variable ANSWER.
 */

function fibonacci()
{
    $result = 1;

    $index = 0;
    $previuos = [1, 2];

    while ($result < 1000000) {
        $val = $previuos[0] + $previuos[1];
        $previuos[$index] = $val;
        $index = $index == 0 ? 1 : 0;
        if ($val % 2 != 0) {
            $result += $val;
        }
    }

    return $result;
}

$ANSWER = fibonacci();

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

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



/**
 * Exercise 2.1 (3 points)
 *
 * In this exercises you should use the former functions `sumArray()` and
 * `calculateArea()`. Create a new function called `calculateMany()` that
 * takes an array with numbers as argument. For every number in the array,
 * call `calculateArea()` and store the result in a new array. The last
 * position in your new array should hold the result of a call to the function
 * `calculateArea()` where you pass the sum of the whole array, you passed as
 * argument. All numbers in the resulting array should be rounded up to
 * nearest integer.
 *
 * Answer with a call to `calculateMany()` with the array `27, 47, 5, 32, 46`.
 *
 *
 * Write your code below and put the answer into the variable ANSWER.
 */

function calculateMany($arr)
{
    $result = array();

    foreach ($arr as $value) {
        $area = calculateArea($value);
        array_push($result, ceil($area));
    }

    $bigval = sumArray($arr);
    $area = calculateArea($bigval);
    array_push($result, ceil($area));
    return $result;
}

$ANSWER = calculateMany([27, 47, 5, 32, 46]);

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


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