Rustam-Amirov/php-project-lvl1

View on GitHub
src/Games/Calc.php

Summary

Maintainability
A
0 mins
Test Coverage
<?php

namespace BrainGames\Games\Calc;

use function BrainGames\Core\run;

const GAME_RULE = 'What is the result of the expression?';
const OPERATORS = ['+', '-', '*'];
function calc()
{
    $getGameData = function () {
        $first = rand(1, 100);
        $second = rand(1, 10);
        $indexRandOpearator = array_rand(OPERATORS);
        $randOpearator = OPERATORS[$indexRandOpearator];
        $question = "{$first} {$randOpearator} {$second}";
        $correctAnswer = calculationCorrectAnswer($randOpearator, $first, $second);
        return [
            'question' => $question,
            'correctAnswer' => $correctAnswer
        ];
    };
    run($getGameData, GAME_RULE);
}

function calculationCorrectAnswer($randOpearator, $first, $second)
{
        
    switch ($randOpearator) {
        case '+':
            return $first + $second;
            break;
        case '-':
            return $first - $second;
            break;
        case '*':
            return $first * $second;
            break;
    }
}