My attempts at exercism.org
1# Lucian's Luscious Lasagna
2
3Welcome to Lucian's Luscious Lasagna on Exercism's PHP Track.
4If you need help running the tests or submitting your code, check out `HELP.md`.
5If you get stuck on the exercise, check out `HINTS.md`, but try and solve it without using those first :)
6
7## Introduction
8
9## Basics
10
11For more detailed information about these topics visit the [concept page][exercism-concept].
12
13### General syntax
14
15The PHP opening tag `<?php` marks the start of PHP code.
16All statements must end with a `;` for instruction separation.
17
18```php
19<?php
20
21$message = "Success!"; // Statement correctly ends with `;`
22$message = "I fail." // PHP Parse error: syntax error, [...]
23```
24
25For easier reading, all code examples omit the PHP opening tag `<?php`.
26
27### Comments
28
29Single line comments start with `//`.
30
31```php
32// Single line comment
33```
34
35### Values and Variables
36
37Using the assignment `=` operator, a value may be assigned to a variable.
38Variable names must start with a dollar `$` sign and follow the [naming conventions][exercism-concept-naming-conventions].
39
40```php
41$count = 1; // Assign value of 1
42
43// Strings can be created by enclosing the characters
44// within single `'` quotes or double `"` quotes.
45$message = "Success!";
46```
47
48### Functions and Methods
49
50Values and variables can be passed to functions.
51Function arguments become new variables to hold values passed in.
52Functions may return any value using the keyword `return`.
53
54```php
55function window_height($height)
56{
57 return $height + 10;
58}
59
60window_height(100);
61// => 110
62```
63
64Functions inside classes and their instances are called methods.
65To call a method, the name is preceded by the instance and `->`.
66Methods have access to the special variable `$this`, which refers to the current instance.
67
68```php
69<?php
70
71class Calculator {
72 public function sub($x, $y)
73 {
74 return $this->add($x, -$y); // Calls the method add() of the current instance
75 }
76
77 public function add($x, $y)
78 {
79 return $x + $y;
80 }
81}
82
83$calculator = new Calculator(); // Creates a new instance of Calculator class
84$calculator->sub(3, 1); // Calls the method sub() of the instance stored in $calculator
85// => 2
86```
87
88[exercism-concept]: /tracks/php/concepts/basic-syntax
89[exercism-concept-naming-conventions]: /tracks/php/concepts/basic-syntax#h-naming-conventions
90
91## Instructions
92
93In this exercise you're going to write some code to help you cook a brilliant lasagna from your favorite cooking book.
94
95You have five tasks, all related to the time spent cooking the lasagna.
96
97## 1. Define the expected oven time in minutes
98
99Implement the `expectedCookTime` function in class `Lasagna` that does not take any arguments and returns how many minutes the lasagna should be in the oven.
100According to the cooking book, the expected oven time in minutes is 40:
101
102```php
103<?php
104$timer = new Lasagna();
105$timer->expectedCookTime()
106// => 40
107```
108
109## 2. Calculate the remaining oven time in minutes
110
111Implement the `remainingCookTime` function in class `Lasagna` that takes the actual minutes the lasagna has been in the oven as an argument and returns how many minutes the lasagna still has to remain in the oven, based on the expected oven time in minutes from the previous task.
112
113```php
114<?php
115$timer = new Lasagna();
116$timer->remainingCookTime(30)
117// => 10
118```
119
120## 3. Calculate the preparation time in minutes
121
122Implement the `totalPreparationTime` function in class `Lasagna` that takes the number of layers you added to the lasagna as an argument and returns how many minutes you spent preparing the lasagna, assuming each layer takes you 2 minutes to prepare.
123
124```php
125<?php
126$timer = new Lasagna();
127$timer->totalPreparationTime(3)
128// => 6
129```
130
131## 4. Calculate the total working time in minutes
132
133Implement the `totalElapsedTime` function in class `Lasagna` that takes two arguments: the first argument is the number of layers you added to the lasagna, and the second argument is the number of minutes the lasagna has been in the oven.
134The function should return how many minutes in total you've worked on cooking the lasagna, which is the sum of the preparation time in minutes, and the time in minutes the lasagna has spent in the oven at the moment.
135
136```php
137<?php
138$timer = new Lasagna();
139$timer->totalElapsedTime(3, 20)
140// => 26
141```
142
143## 5. Create a notification that the lasagna is ready
144
145Implement the `alarm` function in class `Lasagna` that does not take any arguments and returns a message indicating that the lasagna is ready to eat.
146
147```php
148<?php
149$timer = new Lasagna();
150$timer->alarm()
151// => "Ding!"
152```
153
154## Source
155
156### Created by
157
158- @neenjaw