Skip to content

Commit 0433ea3

Browse files
committed
commit hotove ukoly
1 parent 3eaca16 commit 0433ea3

5 files changed

+162
-25
lines changed

task-array.js

+58-14
Original file line numberDiff line numberDiff line change
@@ -23,71 +23,115 @@ printArray(numbers);
2323
// b) Function which will print to console the length of array
2424
const printLength = (numbers) => {
2525
// Your code:
26-
26+
console.log(numbers.length);
2727
};
28+
console.log("printLength");
29+
printLength(numbers);
2830

2931
// c) Function which will print to console the first element of array
3032
const printFirstItem = (numbers) => {
3133
// Your code:
32-
34+
console.log(numbers[0]);
3335
};
36+
console.log("printFirstItem");
37+
printFirstItem(numbers);
3438

3539
// d) Function which will print to console the last element
3640
const printLastItem = (numbers) => {
3741
// Your code:
38-
42+
console.log(numbers.slice(-1));
3943
};
40-
44+
console.log("printLastItem");
45+
printLastItem(numbers);
4146
// e) Function which will print to console the largest number (You can check Math functions)
4247
const printLargestItem = (numbers) => {
4348
// Your code:
44-
49+
console.log(Math.max(...numbers));
4550
};
51+
console.log("printLargestItem");
52+
printLargestItem(numbers);
4653

4754
// f) Function which will print to console the smallest number (You can check Math functions)
4855
const printSmallestItem = (numbers) => {
4956
// Your code:
50-
57+
console.log(Math.min(...numbers));
5158
};
59+
console.log("printSmallestItem");
60+
printSmallestItem(numbers);
5261

5362
// g) Function which will print to console the sum of all numbers in array (You can check reduce function)
5463
const printSum = (numbers) => {
5564
// Your code:
56-
65+
let num = 0;
66+
for(let i = 0; i < numbers.length; i++){
67+
num += numbers[i];
68+
}
69+
console.log(num);
5770
};
71+
console.log("printSum");
72+
printSum(numbers);
5873

5974
// h) Function which will print to console the difference between the largest and the smallest number (You can check Math functions)
6075
const printSALDifference = (numbers) => {
6176
// Your code:
62-
77+
let max = Math.max(...numbers);
78+
let min = Math.min(...numbers);
79+
console.log(max - min);
6380
};
81+
console.log("printSALDifference");
82+
printSALDifference(numbers);
6483

6584
// i) Function which will print to console the average of all numbers (You can check reduce function)
6685
const printAverage = (numbers) => {
6786
// Your code:
68-
69-
87+
let num = 0;
88+
let count = 0;
89+
for(let i = 0; i < numbers.length; i++){
90+
num += numbers[i];
91+
count++;
92+
}
93+
console.log(num / count);
7094
};
95+
console.log("printAverage");
96+
printAverage(numbers);
7197

7298
// j) Function which will print to console the index of largest number (You can check Math functions)
7399
const printLargestsIndex = (numbers) => {
74100
// Your code:
75-
101+
let maxNum = Math.max(...numbers);
102+
let maxIndx = numbers.indexOf(maxNum);
103+
console.log(maxIndx);
76104

77105
};
106+
console.log("printLargestsIndex");
107+
printLargestsIndex(numbers);
78108

79109
// k) Function which will print to console the even numbers (not the array of even numbers),
80110
// if array doesn't contain any even number, show text "Even number isn't in array"
81111
const printEvenNums = (numbers) => {
82112
// Your code:
83-
84-
113+
let bool = false;
114+
for(let i = 0; i < numbers.length; i++){
115+
if(numbers[i] % 2 == 0){
116+
console.log(numbers[i]);
117+
bool = true;
118+
}
119+
}
120+
if(bool == false){
121+
console.log("Even number isnt in array");
122+
}
85123
};
124+
console.log("printEvenNums");
125+
printEvenNums(numbers);
86126

87127
// l) Function which will multiple by 2 every number in array and print the array to console
88128
// Example: printNumsMultipliedBy2([1,2,3]) -> [2,4,6]
89129
const printNumsMultipliedBy2 = (numbers) => {
90130
// Your code:
91-
131+
for(let i = 0; i < numbers.length; i++){
132+
console.log(numbers[i] * 2);
133+
}
92134

93135
};
136+
console.log("printNumsMultipliedBy2");
137+
printNumsMultipliedBy2(numbers);

task-bonus.js

+35-2
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,17 @@
88

99
// Your code:
1010
const drawTriangle = (length = 5) => {
11-
1211
// ... write code ...
12+
let fet = "";
13+
for(let i = 1; i <= length; i++){
14+
for(let j = 1; j <= i; j++){
15+
fet += "* ";
16+
}
17+
fet += "\n";
18+
}
19+
console.log(fet);
1320
};
21+
drawTriangle();
1422

1523
// 2# ========== BONUS =======================
1624
// Write function which will (with cycles) display this (keep in mind that there is no space after the last char):
@@ -29,7 +37,20 @@ const drawTriangle = (length = 5) => {
2937
// Your code:
3038
const drawJavascriptWord = (word = "javascript") => {
3139
// ... write code ...
40+
let fet = "";
41+
for(let i = 0; i < word.length; i++){
42+
for(let j = 0; j < word.length - i - 1; j++){
43+
fet += "* ";
44+
}
45+
for(let j = word.length - i - 1; j < word.length; j++){
46+
fet += word[j].toUpperCase() + ((j < word.length - 1) ? " " : "");
47+
}
48+
fet += "\n";
49+
}
50+
console.log(fet);
51+
3252
};
53+
drawJavascriptWord();
3354

3455

3556
// 3# ========== BONUS =======================
@@ -48,5 +69,17 @@ const drawJavascriptWord = (word = "javascript") => {
4869

4970
// Your code:
5071
const getVehiclesAndTopSpeed = (vehicles) => {
51-
72+
let topSpeedVehicles = [];
73+
for(let i = 0; i < vehicles.length; i++){
74+
let vehicle = vehicles[i];
75+
let topSpeed = Math.max(...vehicle.measuredSpeeds);
76+
topSpeedVehicles.push({ name: vehicle.name, topSpeed: topSpeed});
77+
}
78+
console.log(topSpeedVehicles);
5279
};
80+
const vehicles = [
81+
{ name: "Executor Star Dreadnought", measuredSpeeds: [555, 545, 577, 600] },
82+
{ name: "T-47 Airspeeder", measuredSpeeds: [300, 311, 299, 350] },
83+
{ name: "AT-AT", measuredSpeeds: [20, 21, 20, 19] },
84+
];
85+
getVehiclesAndTopSpeed(vehicles);

task-cycles.js

+24
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,14 @@
88
// Your code:
99
const arrayOfMultiples = (num, length) => {
1010
// ... write code ...
11+
let array = [];
12+
for(let i = 1; i <= length; i++){
13+
array.push(num * i);
14+
}
15+
console.log(array);
1116
};
17+
console.log("arrayOfMultiples");
18+
arrayOfMultiples(7, 5);
1219

1320
// 2 =================================
1421
// Change direction of array
@@ -21,7 +28,10 @@ const arrayOfMultiples = (num, length) => {
2128
// Your code:
2229
const changeDirection = (array) => {
2330
// ... write code ...
31+
console.log(array.reverse());
2432
};
33+
console.log("changeDirection");
34+
changeDirection([0, 1, 2, 3]);
2535

2636
// 3 =================================
2737
// Create function that takes two arrays and return object with two keys - bigger array, sum all numbers
@@ -32,4 +42,18 @@ const changeDirection = (array) => {
3242
// Your code:
3343
const biggerArray = (array1, array2) => {
3444
// ... write code ...
45+
let bigger;
46+
if(array1.length > array2.length){
47+
bigger = array1;
48+
}else{
49+
bigger = array2;
50+
}
51+
let res = 0;
52+
for(let i = 0; i < bigger.length; i++){
53+
res += bigger[i];
54+
}
55+
console.log({"array": bigger,"sum":res});
56+
3557
};
58+
console.log("biggerArray");
59+
biggerArray([1,2,3,4,5], [50,50]);

task-object.js

+22-6
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@
1010

1111
// Your code:
1212
const volumeOfBox = (obj) => {
13-
13+
console.log(obj.width * obj.length * obj.height);
1414
};
15+
console.log("volumeOfBox");
16+
volumeOfBox({ width: 2, length: 5, height: 1 });
1517

1618
// 2 ----
1719
// Create a function that takes strings - firstname, lastname, age, and return object with firstname, lastname, age, yearOfBirth
@@ -20,9 +22,11 @@ const volumeOfBox = (obj) => {
2022

2123
// Your code:
2224
const personObject = (firstname, lastname, age) => {
23-
25+
console.log({ firstName: firstname, lastName: lastname, ageYears: age, yearOfBirth: 2024 - age});
2426

2527
};
28+
console.log("personObject");
29+
personObject("Obi-wan", "Kenobi", "40");
2630

2731
// 3 ----
2832
// Create the function that takes an array with objects and returns the sum of people's budgets.
@@ -35,9 +39,18 @@ const personObject = (firstname, lastname, age) => {
3539

3640
//Your code:
3741
const getBudgets = (persons) => {
38-
42+
let res = 0;
43+
for(let i = 0; i < persons.length; i++){
44+
res += persons[i].budget;
45+
};
46+
console.log(res);
3947
};
40-
48+
console.log("getBudgets");
49+
getBudgets([
50+
{ name: "John", age: 21, budget: 23000 },
51+
{ name: "Steve", age: 32, budget: 40000 },
52+
{ name: "Martin", age: 16, budget: 2700 }
53+
])
4154
// 4 ----
4255
// Create function that takes array of cars and sort them by price
4356
// Example
@@ -46,6 +59,9 @@ const getBudgets = (persons) => {
4659

4760
// Your code:
4861
const sortVehiclesByPrice = (vehicles) => {
49-
50-
62+
vehicles.sort((a, b) => a.price - b.price);
63+
console.log(vehicles);
5164
};
65+
const vehicles = [{name: "Executor Star Dreadnought", price: 999}, {name: "T-47 Airspeeder", price: 5}, {name: "AT-AT", price : 20}]
66+
console.log("sortVehiclesByPrice");
67+
sortVehiclesByPrice(vehicles);

task-strings.js

+23-3
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@
88

99
// Your code:
1010
const parametrize = (input) => {
11-
11+
return input.toLowerCase().replace(/ /g, '-');
1212
};
13+
console.log("parametrize");
14+
console.log(parametrize('Javascript is the best'));
1315

1416
// 2 =================================
1517
// Write function that takes object and return sentence
@@ -19,8 +21,15 @@ const parametrize = (input) => {
1921

2022
// Your code:
2123
const giveSentenceForPerson = (obj) => {
22-
24+
let name = obj.name;
25+
let position = obj.position;
26+
let age = obj.age;
27+
let location = obj.location;
28+
29+
console.log(`Hello ${name} from ${location}! It is nice to meet you! You look awesome for your ${age}, young ${position}!`);
2330
};
31+
console.log("giveSentenceForPerson");
32+
giveSentenceForPerson({name: "Obi-wan", position: "Jedi", age: 40, location: "Starwars universe"});
2433

2534
// 3 =================================
2635
// Write function that takes string and replace all `a`, `e`, `i`, `o` with *
@@ -29,5 +38,16 @@ const giveSentenceForPerson = (obj) => {
2938

3039
// Your code:
3140
const replace = (input) => {
32-
41+
const vowels = ['a', 'e', 'i', 'o'];
42+
let res = '';
43+
for(let i = 0; i < input.length; i++){
44+
if(vowels.includes(input[i])){
45+
res += '*';
46+
}else{
47+
res += input[i];
48+
}
49+
}
50+
console.log(res);
3351
};
52+
console.log("replace");
53+
replace("Hello Javascript");

0 commit comments

Comments
 (0)