Skip to content

Commit 244101c

Browse files
committed
Chapter 2.1 - Grammarly + Upper Letter Titles
1 parent a4a1c3c commit 244101c

File tree

1 file changed

+18
-171
lines changed

1 file changed

+18
-171
lines changed

chapter-02-simple-calculations.md

Lines changed: 18 additions & 171 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ After processing, the data is stored again in variables (i.e. somewhere in the m
1717

1818
## Data Types and Variables
1919

20-
In programming each variable stores a certain **value** of a particular **type**. For example, data types can be a **number**, **string** (text), **boolean** type, **data**, **list**, etc.
20+
In programming each variable stores a certain **value** of a particular **type**. For example, data types can be a **number**, **string** (text), a **boolean** type, **data**, **list**, etc.
2121
Here are some examples of data types and values for them:
2222
- **number** - type of number: 1, 42, -5, 3.14, NaN, …
2323
- **string** - type of text (string): 'Hello', "Hi", 'Beer', …
@@ -54,7 +54,7 @@ function sum([arg1, arg2]) {
5454

5555
Let's note that the arguments **`arg1`** and **`arg2`** can be a different data type than the one we want. That's why it's necessary to convert them into a suitable one. If it's not done for the program **each number** will be just a **string** with which **we can't do operations** arithmetic operations.
5656

57-
### Problem: Calculating a Square Area with Length of a Side **a**
57+
### Problem: Square Area
5858

5959
For example, let's look at the following function which reads an integer from the console, multiplies it by itself (squares it), and prints the result from the multiplication. That's how we can calculate square area by side length:
6060

@@ -127,7 +127,7 @@ function sum([arg1, arg2]) {
127127
}
128128
```
129129

130-
### Problem: Converting Inches into Centimeters
130+
### Problem: Inches to Centimeters
131131

132132
Let's write a function that reads a floating-point number in inches and converts it to centimeters::
133133

@@ -150,7 +150,7 @@ Test your solution here: [https://judge.softuni.org/Contests/Practice/Index/927#
150150

151151
## Reading a Text Input
152152

153-
Same with other data types, to read a **string** it's necessary to **define an argument** to our function and after that to assign it to a variable:
153+
Same with other data types, to read a **string** it's necessary to **define an argument** to our function and after that assign it to a variable:
154154

155155
```JavaScript
156156
function print([arg1]) {
@@ -314,7 +314,7 @@ let expr = (3 + 5) * (4 – 2);
314314

315315
The standard rule for priorities of arithmetic operations is applied: **multiplying and dividing are always done before adding and subtracting**. In the case of an **expression in brackets, it is calculated first** but we already know all of that from the school math.
316316

317-
### Problem: Calculating Trapezoid Area
317+
### Problem: Trapeziod Area
318318

319319
Let's write a program that inputs the lengths of the two bases of a trapezoid and its height (one floating-point number per line) and calculates the **trapezoid area** by the standard math formula:
320320

@@ -372,7 +372,7 @@ Math.round(5.539); // 6
372372
(123.512).toFixed(0); // 124
373373
```
374374

375-
### Problem: Circle Area and Perimeter
375+
### Problem: Circle Area and Perimeter
376376

377377
Let's write a function that receives an input of **the radius r** of a circle and **calculates the area and the perimeter** of the circle.
378378

@@ -441,16 +441,6 @@ calculateRectangleArea([60, 20, 10, 50]); // 1500
441441

442442
Test your solution here: [https://judge.softuni.org/Contests/Practice/Index/927#6](https://judge.softuni.org/Contests/Practice/Index/927#6).
443443

444-
445-
## What Have We Learned from This Chapter?
446-
447-
Let's summarize what we learned from this chapter of the book:
448-
- **Reading an user input**: **`function sum([number1, number2])`**.
449-
- **Converting to number**: **`let num = parseInt(arg1)`**, **`let num = parseFloat(arg1)`**.
450-
- **Aritmetic operations** and using the relevant **aritmetic operators** [+, -, \*, /, ()]: **`let sum = 5 + 3`**.
451-
- **Print text by using concatenation**: **```console.log(`3 + 5 = ${3 + 5}`)```**.
452-
- The different types of **rounding** numbers: **`Math.ceil()`**, **`Math.trunc()`**, **`Math.floor()`** and **`.toFixed()`**
453-
454444
## Exercises: Simple Calculations
455445

456446
Let's strengthen the knowledge gained throughout this chapter with a few more exercises.
@@ -471,158 +461,6 @@ Enter a **significant name** and expansion **.js** to our file, then press the b
471461

472462
![](assets/chapter-2-1-images/00.Visual-studio-03.png)
473463

474-
### Problem: Calculating Square Area
475-
476-
The first exercise from this topic is the following: write a function that **receives an integer `a` and calculates the area** of a square with side **`a`**. The problem is trivially easy: **inputs a number** as an argument of the function, **multiply it by itself**, and **prints a result** on the console.
477-
478-
#### Hints and Guidelines
479-
480-
We have a correctly named empty file. What remains is to write the **code for solving the problem**. For this purpose, we write the following code:
481-
482-
![](assets/chapter-2-1-images/01.Calculate-square-area-01.png)
483-
484-
The code defines a function **`calculateSquareArea()`**, which receives one argument **`arg1`**. Since the argument is expected to be an integer, convert arguments with the method **`parseInt()`** and then calculate the area: **`area = a * a`**. At the end print the value of the variable **`area`**.
485-
For **testing**, it's necessary to **call the function** in the same file with a random parameter and then to start the program by pressing [**Ctrl + F5**]:
486-
487-
![](assets/chapter-2-1-images/01.Calculate-square-area-02.png)
488-
489-
#### Testing in The Judge System
490-
491-
Test your solution here: [https://judge.softuni.org/Contests/Practice/Index/927#0](https://judge.softuni.org/Contests/Practice/Index/927#0).
492-
493-
494-
You must get 100 points (completely correct solution):
495-
496-
![](assets/chapter-2-1-images/01.Calculate-square-area-03.png)
497-
498-
![](assets/chapter-2-1-images/01.Calculate-square-area-04.png)
499-
500-
501-
### Problem: From Inches to Centimeters
502-
503-
Write a function that **accepts a number** (not necessarily an integer) and converts the number from **inches to centimeters**. For this purpose, **it multiplies the inches by 2.54** (because 1 inch = 2.54 centimeters).
504-
505-
#### Hints and Guidelines
506-
507-
First, we create a **new file** in the folder with the other solutions - in Visual Studio Code we choose [**File**] -> [**New file**]. We save the file with the name **convertInchesToCentimeters.js** and press the button [**Save**]. After that we have to write **the program code**:
508-
509-
![](assets/chapter-2-1-images/02.Inches-to-centimeters-01.png)
510-
511-
**We call the function** with parameter **2** and start the problem with [**Ctrl + F5**]:
512-
513-
![](assets/chapter-2-1-images/02.Inches-to-centimeters-02.png)
514-
515-
Let's test it with floating numbers, for example, **4.5**:
516-
517-
![](assets/chapter-2-1-images/02.Inches-to-centimeters-03.png)
518-
519-
#### Testing in The Judge System
520-
521-
Test your solution here: [https://judge.softuni.org/Contests/Practice/Index/927#1](https://judge.softuni.org/Contests/Practice/Index/927#1).
522-
523-
The solution should be taken as completely correct:
524-
525-
![](assets/chapter-2-1-images/02.Inches-to-centimeters-04.png)
526-
527-
528-
### Problem: Greeting by Name
529-
530-
Write a function, which **takes as an argument a human name** and prints **`Hello, <name>!`**, where **`<name>`** is the name entered earlier.
531-
532-
#### Hints and Guidelines
533-
534-
Again we create a **new file** in the folder with the other solutions and we save the file with the name **sayHello.js**. After that, we have to write the program code. If you find it difficult, you can use the sample code below:
535-
536-
![](assets/chapter-2-1-images/03.Greeting-by-name-01.png)
537-
538-
**We call the function** with an example parameter and **start the program** with [**Ctrl+F5**], to test if it works properly:
539-
540-
![](assets/chapter-2-1-images/03.Greeting-by-name-02.png)
541-
542-
#### Testing in The Judge System
543-
544-
Test your solution here: [https://judge.softuni.org/Contests/Practice/Index/927#2](https://judge.softuni.org/Contests/Practice/Index/927#2).
545-
546-
547-
### Problem: Concatenating Text and Numbers
548-
549-
Write a function that receives as an argument the name, surname, age and city and print a message of the following type: **`You are <firstName> <lastName>, a <age>-years old person from <town>`**.
550-
551-
#### Hints and Guidelines
552-
553-
Similarly, we create a new file and name it **printInfo.js**. which prints the message described in the condition of the task, is purposefully blurred for you to think of a way to finish it yourself:
554-
555-
![](assets/chapter-2-1-images/04.Concatenate-data-01.png)
556-
557-
The solution should be tested locally by calling the function with sample values and starting the program with [**Ctrl+F5**].
558-
559-
#### Testing in The Judge System
560-
561-
Test your solution here: [https://judge.softuni.org/Contests/Practice/Index/927#3](https://judge.softuni.org/Contests/Practice/Index/927#3).
562-
563-
564-
### Problem: Trapezoid Area
565-
566-
Write a function that receives three arguments **b1, b2, and h and calculate the trapezoid area** with bases **b1 and b2 and height h. The formula for trapezoid area is (b1 + b2) * h / 2**.
567-
568-
The figure below shows a trapezoid with bases 8 and 13 and height 7. It has an area **(8 + 13) * 7 / 2 = 73.5**.
569-
570-
![](assets/chapter-2-1-images/05.Trapezoid-area-01.png)
571-
572-
#### Hints and Guidelines
573-
574-
We should add a new file in Visual Studio Code again with the name **calculateTrapezoidArea.js** and write a code that reads the inputs from the function's arguments, calculates the trapezoid area, and prints it. The code in the picture is purposely blurred, for you to give it a thought and finish it yourself:
575-
576-
![](assets/chapter-2-1-images/05.Trapezoid-area-02.png)
577-
578-
**Test** your solution locally by calling the function and starting the program with [**Ctrl+F5**].
579-
580-
#### Testing in The Judge System
581-
582-
Test your solution here: [https://judge.softuni.org/Contests/Practice/Index/927#4](https://judge.softuni.org/Contests/Practice/Index/927#4).
583-
584-
585-
### Problem: Circle Perimeter and Area
586-
587-
Write a function, that receives an argument **number r** and calculates and prints **the area and perimeter of a circle** with **radius r**.
588-
589-
#### Sample Input and Output
590-
591-
| Input | Output |
592-
| ----- | --------------------------------------------------------- |
593-
| 3 | Area = 28.2743338823081 <br> Perimeter = 18.8495559215388 |
594-
| 4.5 | Area = 63.6172512351933 <br> Perimeter = 28.2743338823081 |
595-
596-
#### Hints and Guidelines
597-
598-
For the calculations you may use the following formulas:
599-
- **`Area = Math.PI * r * r`**.
600-
- **`Perimeter = 2 * Math.PI * r`**.
601-
602-
#### Testing in The Judge System
603-
604-
Test your solution here: [https://judge.softuni.org/Contests/Practice/Index/927#5](https://judge.softuni.org/Contests/Practice/Index/927#5).
605-
606-
607-
### Problem: Rectangle Area
608-
609-
A **Rectangle** is defined by the **coordinates** of both of its opposite corners (x1, y1) – (x2, y2). Calculate its **area and perimeter**. **The input** is taken as a function argument. The numbers **x1, y1, x2, and y2** are given one per line. **The output** is printed on the console and it has to contain two lines, each with one number – the area and the perimeter.
610-
611-
![](assets/chapter-2-1-images/00.Rectangle-area-01.png)
612-
613-
#### Sample Input and Output
614-
615-
| Input | Output |
616-
| ------------------------------------ | ------------------- |
617-
| 60<br>20<br>10<br>50 | 1500<br>160 |
618-
| 30<br>40<br>70<br>-10 | 2000<br>180 |
619-
| 600.25<br>500.75<br>100.50<br>-200.5 | 350449.6875<br>2402 |
620-
621-
#### Testing in The Judge System
622-
623-
Test your solution here: [https://judge.softuni.org/Contests/Practice/Index/927#6](https://judge.softuni.org/Contests/Practice/Index/927#6).
624-
625-
626464
### Problem: Triangle Area
627465

628466
Write a function that receives arguments which are **a side and a height of a triangle** and calculates its area. Use **the formula** for triangle area: **area = a * h / 2**. Round the result to **2 digits after the decimal point using `area.toFixed(2)`**.
@@ -641,7 +479,7 @@ Write a function that receives arguments which are **a side and a height of a tr
641479
Test your solution here: [https://judge.softuni.org/Contests/Practice/Index/927#7](https://judge.softuni.org/Contests/Practice/Index/927#7).
642480

643481

644-
### Problem: Converter – from °C Degrees to °F Degrees
482+
### Problem: Celsius to Fahrenheit
645483

646484
Write a function that reads **degrees on the Celsius scale** (°C) and converts them to **degrees on the Fahrenheit scale** (°F). Look on the Internet for a proper [formula](https://bfy.tw/3rGh "Search on Google") to do the calculations. Round the result to **2 digits after the decimal point**. Here are a few examples:
647485

@@ -659,7 +497,7 @@ Write a function that reads **degrees on the Celsius scale** (°C) and converts
659497
Test your solution here: [https://judge.softuni.org/Contests/Practice/Index/927#8](https://judge.softuni.org/Contests/Practice/Index/927#8).
660498

661499

662-
### Problem: Converter - Radians to Degrees
500+
### Problem: USD to BGN
663501

664502
Write a function that reads **an angle in [radians](https://en.wikipedia.org/wiki/Radian)** (**`rad`**) and converts it in **[degrees](https://en.wikipedia.org/wiki/Degree_(angle))** (`deg`). Look for a proper formula on the Internet. The number **π** in **JavaScript** programs is available through **``Math.PI``**. Round the result to the nearest integer using the **``Math.round(…)``** method.
665503

@@ -717,7 +555,7 @@ Write a function for the **conversion of money from one currency into another**.
717555
Test your solution here: [https://judge.softuni.org/Contests/Practice/Index/927#11](https://judge.softuni.org/Contests/Practice/Index/927#11).
718556

719557

720-
### Problem: ** Date Calculations – 1000 Days on The Earth
558+
### Problem: \*\* 1000 Days After Birth
721559

722560
Write a function that reads **a birth date** in format **`dd-MM-yyyy`** and calculates the date on which **1000 days** are turned since this birth date and prints it in the same format.
723561

@@ -938,3 +776,12 @@ Test the application by opening the project folder in **explorer** and launching
938776
5. Done the application.
939777

940778
If you have any difficulties, ask in **the Softuni Reddit**: https://www.reddit.com/r/softuni/.
779+
780+
## What Have We Learned from This Chapter?
781+
782+
Let's summarize what we learned from this chapter of the book:
783+
- **Reading an user input**: **`function sum([number1, number2])`**.
784+
- **Converting to number**: **`let num = parseInt(arg1)`**, **`let num = parseFloat(arg1)`**.
785+
- **Aritmetic operations** and using the relevant **aritmetic operators** [+, -, \*, /, ()]: **`let sum = 5 + 3`**.
786+
- **Print text by using concatenation**: **```console.log(`3 + 5 = ${3 + 5}`)```**.
787+
- The different types of **rounding** numbers: **`Math.ceil()`**, **`Math.trunc()`**, **`Math.floor()`** and **`.toFixed()`**

0 commit comments

Comments
 (0)