Skip to content

Commit 5842c3d

Browse files
committed
corrected, now they are correctly done
1 parent bb03e7e commit 5842c3d

File tree

10 files changed

+199
-83
lines changed

10 files changed

+199
-83
lines changed

lab3/db.json

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"posts": [
3+
{
4+
"id": 1,
5+
"title": "json-server",
6+
"author": "typicode"
7+
}
8+
],
9+
"comments": [
10+
{
11+
"id": 1,
12+
"body": "some comment",
13+
"postId": 1
14+
}
15+
],
16+
"profile": {
17+
"name": "typicode"
18+
}
19+
}

lab3/zad5/index.html

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,19 @@
99
</head>
1010
<body>
1111

12-
<div id="outer-box" class="clickable">
13-
<span>1</span>
14-
<div id="inner-box" class="clickable">
15-
<span>2</span>
16-
<div id="innerest-box" class="clickable">
17-
<span>3</span>
12+
<div id="outer-box" class="clickable">
13+
<span>1</span>
14+
<div id="inner-box" class="clickable">
15+
<span>2</span>
16+
<div id="innerest-box" class="clickable">
17+
<span>3</span>
18+
</div>
1819
</div>
19-
2020
</div>
21-
22-
</div>
2321
<p id="points">0</p>
24-
<p id="info">Tutaj bedzie informacja</p>
22+
<p id="info1">Tutaj będzie informacja</p>
23+
<p id="info2">-</p>
24+
<p id="info3">-</p>
2525
<div class="buttons">
2626
<button id="reset">Reset</button>
2727
<button id="propagation" class="cur-stop">Stop Propagation</button>

lab3/zad5/script.js

Lines changed: 55 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,33 @@
11
var points = 0;
22
var clicked = false;
3+
var propagation = true;
34

5+
var blue_clicked = false;
6+
var red_clicked = false;
7+
var yellow_clicked = false;
48

5-
function displayInfo(info){
6-
var el = document.getElementById("info");
7-
el.textContent=info;
9+
function clearInfo(){
10+
document.getElementById("info1").textContent = "-";
11+
document.getElementById("info2").textContent = "-";
12+
document.getElementById("info3").textContent = "-";
13+
}
14+
15+
function resetbuttons(){
16+
blue_clicked = false;
17+
red_clicked = false;
18+
yellow_clicked = false;
19+
}
20+
21+
function displayInfo(){
22+
document.querySelector("#info1").textContent= "-"
23+
document.querySelector("#info2").textContent= "-"
24+
document.querySelector("#info3").textContent= "-"
25+
if (blue_clicked)
26+
document.querySelector("#info1").textContent= "Nacisnąłeś niebieski o wartości 1"
27+
if (red_clicked)
28+
document.querySelector("#info2").textContent= "Nacisnąłeś czerwony o wartości 2"
29+
if (yellow_clicked)
30+
document.querySelector("#info3").textContent= "Nacisnąłeś żółty o wartości 5"
831
}
932

1033
function changePoints(points){
@@ -16,24 +39,35 @@ function changePoints(points){
1639
function yellowClick(){
1740
if (points>50 || document.getElementById("innerest-box").classList.contains("disabled"))
1841
return;
19-
displayInfo("Nacisnąłeś żółty o wartości 5");
2042
changePoints(points + 5);
43+
yellow_clicked = true;
44+
if (!propagation){
45+
displayInfo();
46+
resetbuttons();
47+
}
2148
}
2249

23-
function blueClick(){
24-
if (document.getElementById("outer-box").classList.contains("disabled"))
25-
return;
26-
displayInfo("Nacisnąłeś niebieski o wartości 1");
27-
changePoints(points + 1);
28-
}
2950

3051
function redClick(){
3152
if (points>30 || document.getElementById("inner-box").classList.contains("disabled"))
3253
return;
33-
displayInfo("Nacisnąłeś czerwony o wartości 2");
54+
red_clicked = true;
55+
if (!propagation){
56+
displayInfo();
57+
resetbuttons();
58+
}
59+
3460
changePoints(points + 2);
3561
}
3662

63+
function blueClick(){
64+
if (document.getElementById("outer-box").classList.contains("disabled"))
65+
return;
66+
changePoints(points + 1);
67+
blue_clicked = true;
68+
displayInfo();
69+
resetbuttons();
70+
}
3771
function clickHandler(id){
3872
if(!clicked){
3973
switch(id){
@@ -57,7 +91,8 @@ function clickHandler(id){
5791

5892

5993
}
60-
clicked = true;
94+
if (!propagation)
95+
clicked = true;
6196
}
6297

6398
function clickHandlerReset(){
@@ -70,28 +105,25 @@ function reset(){
70105
clicked = false;
71106
document.getElementById("inner-box").classList.remove("disabled");
72107
document.getElementById("innerest-box").classList.remove("disabled");
73-
displayInfo("Wlasnie zresetowales.");
108+
clearInfo();
109+
resetbuttons();
110+
document.querySelector("#info1").textContent= "Właśnie zresetowałeś"
111+
document.querySelector("#info2").textContent= "-"
112+
document.querySelector("#info3").textContent= "-"
74113
}
75114

76-
77115
function propagationHandler(){
78116
if (this.classList.contains("cur-stop")){
117+
propagation = false;
79118
this.classList.remove("cur-stop");
80119
this.classList.add("cur-start");
81-
document.getElementById("outer-box").classList.add("disabled");
82-
document.getElementById("inner-box").classList.add("disabled");
83-
document.getElementById("innerest-box").classList.add("disabled");
84120
this.textContent="Start Propagation";
85121
}
86122
else if (this.classList.contains("cur-start")){
123+
propagation = true;
87124
this.classList.remove("cur-start");
88125
this.classList.add("cur-stop");
89126
this.textContent="Stop Propagation";
90-
document.getElementById("outer-box").classList.remove("disabled");
91-
if (points <=30)
92-
document.getElementById("inner-box").classList.remove("disabled");
93-
if (points <=50)
94-
document.getElementById("innerest-box").classList.remove("disabled");
95127
}
96128
return
97129
}
@@ -105,6 +137,7 @@ document.getElementById("inner-box").onclick = function() {
105137
}
106138

107139
document.getElementById("outer-box").onclick = function() {
140+
console.log("2");
108141
clickHandler(this.id);
109142
clickHandlerReset();
110143
}

lab3/zad5/style.css

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ body{
3636
position:relative;
3737
justify-content: center;
3838
align-items: center;
39-
z-index:10;
39+
z-index:1;
4040

4141
}
4242

@@ -49,7 +49,7 @@ body{
4949
position:relative;
5050
justify-content: center;
5151
align-items: center;
52-
z-index:-1;
52+
z-index:2;
5353
}
5454

5555

@@ -78,7 +78,7 @@ span{
7878
cursor: pointer;
7979
}
8080

81-
#info, #points{
81+
#info1, #info2, #info3, #points{
8282
font-size:48px;
8383
font-weight: 900;
8484
letter-spacing: 2px;

lab3/zad7/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ <h2>e) Wyliczyć czy więcej jest miast powyżej 80000 mieszkańców czy poniże
4141
<section id="f">
4242
<h2>f) Jaka jest średnia powierzchnia miast z powiatów zaczynających się na literkę „P”</h2>
4343
<p id="f-answer"></p>
44+
<p id="f-answer2"></p>
4445
</section>
4546
<script src="script.js"></script>
4647
</body>

lab3/zad7/script.js

Lines changed: 80 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
1-
async function getA() {
1+
async function getData() {
22
var res = await fetch("http://localhost:3000/cities");
33
var json = await res.json();
4+
return json;
5+
}
6+
7+
async function getA(data) {
8+
var json = data;
49

510
var filtered = json.filter(function (entry) {
611
return entry.province === "małopolskie";
@@ -21,36 +26,59 @@ async function getA() {
2126

2227
}
2328

24-
async function getB() {
25-
var res = await fetch("http://localhost:3000/cities?name_like=.*(A|a).*a.*");
26-
var json = await res.json();
29+
function checkDoubleA(word) {
30+
var cnt = 0;
31+
for (var i = 0; i < word.length; i++) {
32+
letter = word.charAt(i)
33+
if (letter == 'A' || letter == 'a')
34+
cnt++;
35+
if (cnt == 2)
36+
break;
37+
}
38+
return cnt == 2
39+
}
40+
41+
async function getB(data) {
42+
var json = data
43+
44+
var filtered = json.filter(function (entry) {
45+
return checkDoubleA(entry.name);
46+
});
2747

2848
var cities = ""
29-
for (var prop in json) {
30-
if (!json.hasOwnProperty(prop)) {
49+
50+
for (var prop in filtered) {
51+
if (!filtered.hasOwnProperty(prop)) {
3152
continue;
3253
}
33-
cities += json[prop].name + ", "
54+
cities += filtered[prop].name + ", "
3455
}
3556
cities = cities.substr(0, cities.length - 2);
3657
cities = cities + "."
37-
3858
document.getElementById("b-answer").textContent = cities;
3959

4060

4161
}
4262

43-
async function getC() {
44-
var res = await fetch("http://localhost:3000/cities?_sort=dentensity&_order=desc&_limit=5");
45-
var json = await res.json();
46-
var city = json[4].name + '.';
47-
document.getElementById("c-answer").textContent = city;
63+
async function getC(data) {
64+
var json = data;
65+
var citiesArray = new Array;
66+
for (var prop in json) {
67+
citiesArray.push([json[prop].name, json[prop].people / json[prop].area]);
68+
}
69+
citiesArray.sort(function (a, b) {
70+
if (a[1] < b[1])
71+
return 1
72+
else if (a[1] > b[1])
73+
return -1
74+
return 0
75+
});
76+
document.getElementById("c-answer").textContent = citiesArray[4][0];
4877

4978
}
5079

51-
async function getD() {
52-
var res = await fetch("http://localhost:3000/cities");
53-
var json = await res.json();
80+
async function getD(data) {
81+
var json = data;
5482

5583
var filtered = json.filter(function (entry) {
5684
return entry.people > 100000;
@@ -65,52 +93,57 @@ async function getD() {
6593
cities = cities.substr(0, cities.length - 2) + "."
6694

6795
document.getElementById("d-answer").textContent = cities;
68-
// var res = fetch("http://localhost:3000/cities",{
69-
// method: 'PATCH',
70-
// headers:{
71-
// 'Content-Type':'application/json'
72-
// },
73-
// body: JSON.stringify(DATA_WHICH_WE_WANT_TO_SEND)
74-
// })
7596
}
7697

77-
async function getE() {
78-
var res1 = await fetch("http://localhost:3000/cities?people_gte=80000");
79-
var json1 = await res1.json();
80-
81-
var res2 = await fetch("http://localhost:3000/cities?people_lte=80000");
82-
var json2 = await res2.json();
83-
84-
document.getElementById("e-answer1").textContent = "Miast powyżej 80000 mieszkańców jest: " + json1.length;
85-
document.getElementById("e-answer2").textContent = "Miast poniżej 80000 mieszkańców jest: " + json2.length;
86-
if (json1.length > json2.length) {
98+
async function getE(data) {
99+
var json = data;
100+
var cntMore = 0;
101+
var cntLess = 0;
102+
for (var prop in json) {
103+
if (json[prop].people > 80000)
104+
cntMore++;
105+
else
106+
cntLess++;
107+
}
108+
document.getElementById("e-answer1").textContent = "Miast powyżej 80000 mieszkańców jest: " + cntMore;
109+
document.getElementById("e-answer2").textContent = "Miast poniżej 80000 mieszkańców jest: " + cntLess;
110+
if (cntMore > cntLess) {
87111
document.getElementById("e-answer3").textContent = "Zatem: więcej jest miast powyżej 80k mieszkańców.";
88112
} else {
89113
document.getElementById("e-answer3").textContent = "Zatem: więcej jest miast poniżej 80k mieszkańców.";
90114
}
91115

92116
}
93117

94-
async function getF() {
95-
var res = await fetch("http://localhost:3000/cities?township_like=P*");
96-
var json = await res.json();
97-
118+
async function getF(data) {
119+
var json = data;
98120
var area_sum = 0;
99-
json.forEach(element => {
100-
area_sum += element.area;
101-
});
102121

103-
document.getElementById("f-answer").textContent = area_sum / json.length;
122+
var citiesP = new Array;
123+
for (var prop in json) {
124+
if (json[prop].township.charAt(0) == 'P')
125+
citiesP.push([json[prop].area, json[prop].township]);
126+
}
127+
console.log(citiesP);
128+
citiesPstr = ""
129+
citiesP.forEach(element => {
130+
area_sum += element[0];
131+
citiesPstr += element[1] + ", "
132+
});
133+
citiesPstr = citiesPstr.substr(0, citiesPstr.length - 2) + "."
134+
document.getElementById("f-answer").textContent = area_sum / citiesP.length;
135+
document.getElementById("f-answer2").textContent = "Są to powiaty (zaczynające się na dużą literę P): " + citiesPstr
104136

105137
}
106138

107139
async function loadSite() {
108-
getA();
109-
getB();
110-
getC();
111-
getD();
112-
getE();
113-
getF();
140+
var json = await getData();
141+
getA(json);
142+
getB(json);
143+
getC(json);
144+
getD(json);
145+
getE(json);
146+
getF(json);
114147
}
115148

116149
loadSite();

0 commit comments

Comments
 (0)