You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This **function** has the task to print a header, that is a series of the symbol **`-`**. Because of this, the name of the function is **`printHeader`**. The round brackets **`( `** and **`)`** are always after the name, no matter how we have named the functions. Later we will take a look at how we have to name a function we are working with. For now, we will only say that the **name of the function must define the action** that is doing.
23
+
This **function** has the task to print a header, which is a series of the symbol **`-`**. Because of this, the name of the function is **`printHeader`**. The round brackets **`( `** and **`)`** are always after the name, no matter how we have named the functions. Later we will take a look at how we have to name a function we are working with. For now, we will only say that the **name of the function must define the action** that is doing.
24
24
25
25
The **body** of the function consists of **programming code**, which is located between the curly brackets **`{`** and **`}`**. Between them, we place code, that will solve our problem, described by the name of the function.
26
26
@@ -30,13 +30,13 @@ Up to this moment, we have found out that, functions help us with **separating l
30
30
31
31
Using functions we **escape repeating** of programming code. **Repeating** code is a **bad practice** because it makes **harder maintenance** for the programmer to do which leads to errors. If one part of code exists in our program more than once and we have to fix it, we will have to change every occurrence of repeating code. The probability of us forgetting one of the repeated places is high, which will lead to incorrect behavior of our program. This is exactly why it's a **good practice** to define a fragment that will be used **more than once** as a **separate function**.
32
32
33
-
Functions offer us a **good method** to use **code several times**. With solving more and more exercises, we will come to the conclusion that using already defined functions saves us a lot of time and effort.
33
+
Functions offer us a **good method** to use **code several times**. With solving more and more exercises, we will conclude that using already defined functions saves us a lot of time and effort.
34
34
35
35
## Declaring Functions
36
36
37
37
In JavaScript language we can **define** functions everywhere, using the same way we define variables. Declaring represents the **registration of a function** inside a program and to be recognized inside it.
38
38
39
-
JavaScript isn't **strongly typed** language. That's why when we **declare a function** it doesn't have a type(string, number, array, etc.), which other programming language methods have.
39
+
JavaScript isn't a **strongly typed** language. That's why when we **declare a function** it doesn't have a type(string, number, array, etc.), which other programming language methods have.
40
40
41
41
There are two ways, to declare a function in JavaScript - **function declaration** and **Function expression**.
42
42
@@ -72,7 +72,7 @@ The difference between **Function declaration** and **Function expression** is v
72
72
In theory, this means that we can **call a function** that is declared with **function declaration** even before it was declared in the previous rows. If we try to use **function expression** the program will **throw an error** that the function is not declared yet.
73
73
74
74
## Invoking a Function
75
-
Invoking a function is a**start of the execution of a code** that is located inside the body of a function. We call it by typing the **name** of the function followed by **`()`** and **`;`** to end the row. Here is an example:
75
+
Invoking a function is the**start of the execution of a code** that is located inside the body of a function. We call it by typing the **name** of the function followed by **`()`** and **`;`** to end the row. Here is an example:
@@ -86,9 +86,9 @@ A Function can be invoked from the **body of another function** which is **not**
86
86
87
87
A Function can be called from **its own body**. This is called **recursion** and you can find more about it on [Wikipedia](https://en.wikipedia.org/wiki/Recursion_(computer_science)) or google it.
88
88
89
-
### Problem: empty cash receipt
89
+
### Problem: Blank Receipt
90
90
91
-
Write a function, that prints empty cash receipt. The function must call another three functions: one to print the title, one for the main part, and the last for the bottom part.
91
+
Write a function, that prints an empty cash receipt. The function must call another three functions: one to print the title, one for the main part, and the last for the bottom part.
92
92
93
93
|Part of cash receipt|Text|
94
94
|---|---|
@@ -153,7 +153,7 @@ Let's look at another example:
153
153
154
154
In this example, we call the function **`printNumbers(...)`** but this time we give only 1 parameter instead of the **declared** 2 parameters. All parameters that are not **set as values** will automatically get **`undefined`** values. In our case the variable.
155
155
156
-
### Problem: Symbol of Integer
156
+
### Problem: Sign of Integer Number
157
157
158
158
Create a function that checks if a number is a positive or negative number.
159
159
@@ -196,7 +196,7 @@ Our next example shows the use of optional parameters:
196
196
197
197
When we are not **setting value** for the parameter, he will **get the value** that we have given him when declaring the function.
198
198
199
-
### Problem: Print of Triangle
199
+
### Problem: Printing Triangle
200
200
201
201
Create a function that will create a triangle as shown in the example.
202
202
@@ -230,7 +230,7 @@ In the end, we will print the **bottom part** from the triangle but this time wi
230
230
231
231
Test your solution here: [https://judge.softuni.org/Contests/Practice/Index/943#2](https://judge.softuni.org/Contests/Practice/Index/943#2).
232
232
233
-
### Problem: Draw a Filled Square.
233
+
### Problem: Draw a Filled Square
234
234
235
235
Draw a square with side **`n`**, as shown in the example.
236
236
@@ -261,7 +261,7 @@ Test your solution here: [https://judge.softuni.org/Contests/Practice/Index/943#
261
261
262
262
## Return Result From a Function
263
263
264
-
Up to this point, we have viewed functions that do a specific task. For example printing a text, number or a figure on the console. There is another type of function that can **return** a **result** from their task. We can return a result of the multiplication of two numbers. We will look at this type of function in this part.
264
+
Up to this point, we have viewed functions that do a specific task. For example printing a text, number, or a figure on the console. There is another type of function that can **return** a **result** from their task. We can return a result of the multiplication of two numbers. We will look at this type of function in this part.
265
265
266
266
### Operator Return
267
267
To get a result from a function we use the operator **`return`**. He must be **used inside the body** of a function. The **`return`** will tell the program to **stop the execution** of the function and **return** a corresponding value. This value is defined after the **`return`** word.
@@ -270,7 +270,7 @@ In the example underneath we have a **function** that gets the first and last na
**There are cases** in which **`return`** can be called from different places inside the function, but if only the **conditions** are met.
273
+
**There are cases** in which **`return`** can be called from different places inside the function, but only if the **conditions** are met.
274
274
275
275
In the example below, we have a function that compares 2 numbers and **returns** the result which can be **`-1`**, **`0`**, or **`1`** if the first argument is smaller, equal, or bigger than the second argument. The function uses the operator **`return`** on 3 different places. It returns different values depending on the logic of the code.
276
276
@@ -305,7 +305,7 @@ The third is to **pass** the result from one function to **another function**:
After we have done our calculations, all we are left to do is calling the function.
349
+
After we have done our calculations, all we are left to do is invoke the function.
350
350
351
351
#### Testing in The Judge System
352
352
@@ -402,7 +402,7 @@ We see that in the previous example, **`solve()`** function has **another** decl
402
402
### Why Should We Use Local Functions?
403
403
404
404
With time and practice we will find out that when we type code, we often need functions, which
405
-
we might need only once or another function is becoming too long. We have already said that when one function has too many rows of code it is very hard to read, support, and understand. In those cases, we can declare another function which we will use even only once. This helps the code to be cleaner and reduces the chance of making a mistake in the programming code.
405
+
we might need only once or another function is becoming too long. We have already said that when one function has too many rows of code it is very hard to read, support, and understand. In those cases, we can declare another function that we will use even only once. This helps the code to be cleaner and reduces the chance of making a mistake in the programming code.
406
406
407
407
### Declaring Local Functions
408
408
@@ -424,7 +424,7 @@ In this part, we will take a look at some of the **best practices** for writing
424
424
425
425
### Naming Functions
426
426
427
-
When we name a function it's recommended to use **logical names**. This is good because every function must **correspond** to a specific part of our problem. We must take into consideration the **task** which the function will do. This is why it's a good practice **for the name to describes the purpose of the function**.
427
+
When we name a function it's recommended to use **logical names**. This is good because every function must **correspond** to a specific part of our problem. We must take into consideration the **task** which the function will do. This is why it's a good practice **for the name to describe the purpose of the function**.
428
428
429
429
It is required for the name of the function to start with a **small letter** and to be a verb or a combination of a verb and a noun. Formatting the names must be done following **Lower Camel Case** convention - **every word except the first to start with an upper letter**. Round brackets
430
430
**`( `** and **`)`** are always after the name of the function.
@@ -510,7 +510,7 @@ In this chapter we have learned base concepts about working with functions:
510
510
511
511
To improve what we have learned we will solve a few exercises. In them, it is required to write functions with specific functionality and then call it with values as shown in the example.
512
512
513
-
### Problem: "Hello, Name!"
513
+
### Problem: Hello, Name!
514
514
515
515
Write a function that takes a name as a parameter and prints on the console "*Hello, \{name\}!*".
516
516
@@ -683,11 +683,11 @@ Write a function **`letterize(number)`**, which reads an integer and prints it i
683
683
684
684
We can first print **the hundreds** as a text – \(the number / 100\) % 10, after that **the tens** – \(the number / 10\) % 10 and at the end **the ones** – \(the number % 10\).
685
685
686
-
The first special case is when the number is exactly **rounded to 100**\(e.g. 100, 200, 300 etc...\). In this case we print "one-hundred", "two-hundred", "three-hundred" etc.
686
+
The first special case is when the number is exactly **rounded to 100**\(e.g. 100, 200, 300, etc\). In this case we print "one-hundred", "two-hundred", "three-hundred" etc.
687
687
688
688
The second special case is when the number formed by the last two digits of the input number is **less than 10**\(e.g. 101, 305, 609, etc...\). In this case, we print "one-hundred and one", "three-hundred and five", "six-hundred and nine" etc.
689
689
690
-
The third special case is when the number formed by the last two digits of the input number is **larger than 10 and smaller than 20**\(e.g. 111, 814, 919, etc.\). In this case, we print "one-hundred and eleven", "eight-hundred and fourteen", "nine-hundred and nineteen" etc.
690
+
The third special case is when the number formed by the last two digits of the input number is **larger than 10 and smaller than 20**\(e.g. 111, 814, 919, etc\). In this case, we print "one-hundred and eleven", "eight-hundred and fourteen", "nine-hundred and nineteen" etc.
0 commit comments