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
@@ -17,7 +17,7 @@ After processing, the data is stored again in variables (i.e. somewhere in the m
17
17
18
18
## Data Types and Variables
19
19
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.
21
21
Here are some examples of data types and values for them:
22
22
-**number** - type of number: 1, 42, -5, 3.14, NaN, …
23
23
-**string** - type of text (string): 'Hello', "Hi", 'Beer', …
@@ -54,7 +54,7 @@ function sum([arg1, arg2]) {
54
54
55
55
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.
56
56
57
-
### Problem: Calculating a Square Area with Length of a Side **a**
57
+
### Problem: Square Area
58
58
59
59
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:
60
60
@@ -127,7 +127,7 @@ function sum([arg1, arg2]) {
127
127
}
128
128
```
129
129
130
-
### Problem: Converting Inches into Centimeters
130
+
### Problem: Inches to Centimeters
131
131
132
132
Let's write a function that reads a floating-point number in inches and converts it to centimeters::
133
133
@@ -150,7 +150,7 @@ Test your solution here: [https://judge.softuni.org/Contests/Practice/Index/927#
150
150
151
151
## Reading a Text Input
152
152
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:
154
154
155
155
```JavaScript
156
156
functionprint([arg1]) {
@@ -314,7 +314,7 @@ let expr = (3 + 5) * (4 – 2);
314
314
315
315
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.
316
316
317
-
### Problem: Calculating Trapezoid Area
317
+
### Problem: Trapeziod Area
318
318
319
319
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:
320
320
@@ -372,7 +372,7 @@ Math.round(5.539); // 6
372
372
(123.512).toFixed(0); // 124
373
373
```
374
374
375
-
### Problem: Circle Area and Perimeter
375
+
### Problem: Circle Area and Perimeter
376
376
377
377
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.
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:
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**]:
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**:
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:
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:
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**.
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:
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.
Test your solution here: [https://judge.softuni.org/Contests/Practice/Index/927#6](https://judge.softuni.org/Contests/Practice/Index/927#6).
624
-
625
-
626
464
### Problem: Triangle Area
627
465
628
466
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
641
479
Test your solution here: [https://judge.softuni.org/Contests/Practice/Index/927#7](https://judge.softuni.org/Contests/Practice/Index/927#7).
642
480
643
481
644
-
### Problem: Converter – from °C Degrees to °F Degrees
482
+
### Problem: Celsius to Fahrenheit
645
483
646
484
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:
647
485
@@ -659,7 +497,7 @@ Write a function that reads **degrees on the Celsius scale** (°C) and converts
659
497
Test your solution here: [https://judge.softuni.org/Contests/Practice/Index/927#8](https://judge.softuni.org/Contests/Practice/Index/927#8).
660
498
661
499
662
-
### Problem: Converter - Radians to Degrees
500
+
### Problem: USD to BGN
663
501
664
502
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.
665
503
@@ -717,7 +555,7 @@ Write a function for the **conversion of money from one currency into another**.
717
555
Test your solution here: [https://judge.softuni.org/Contests/Practice/Index/927#11](https://judge.softuni.org/Contests/Practice/Index/927#11).
718
556
719
557
720
-
### Problem: ** Date Calculations – 1000 Days on The Earth
558
+
### Problem: \*\*1000 Days After Birth
721
559
722
560
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.
723
561
@@ -938,3 +776,12 @@ Test the application by opening the project folder in **explorer** and launching
938
776
5. Done the application.
939
777
940
778
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