Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion 01/assets/js/app.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,28 @@
const a = '4.2';
const b = 9;

console.log(a, b);
console.log(a, b);

console.log("a:", typeof a);
console.log("b:", typeof b);

const operObj = {
sum: parseInt(b) + parseInt(a),
sub: parseInt(b) - parseInt(a),
divd: parseInt(b) / parseInt(a),
mult: parseInt(b) * parseInt(a),
concat: String(a) + parseInt(b),
modulo: parseInt(b) % parseInt(a)
};

for (const key in operObj) {
if (operObj[key] > 20) {
console.log(`Wartość ${key} jest większa od 20`);
} else if (operObj[key] === 20) {
console.log(`Wartość ${key} jest równa 20`)
} else if (operObj[key] < 20) {
console.log(`Wartość ${key} jest mniejsza niż 20`)
} else if (typeof operObj[key] !== "number") {
console.log(`Wartość ${key} nie jest ${typeof operObj[key]}`)
}
};
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

15 changes: 12 additions & 3 deletions 01/index.html
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
<!DOCTYPE html>
<html lang="pl">

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<meta
name="viewport"
content="width=device-width, initial-scale=1.0"
>
<meta
http-equiv="X-UA-Compatible"
content="ie=edge"
>
<title>devmentor.pl - JS BASICS - #01</title>
</head>

<body>

<script src="./assets/js/app.js"></script>
</body>

</html>
36 changes: 33 additions & 3 deletions 02/app.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,37 @@

/* rozwiązanie z pętlą for */
const x = 4;
const x = parseInt(prompt('Podaj liczbę!'));


for (let i = 1; i < 10; i++) {
const result = x * i;
console.log(`${x} * ${i} = ${result}`);
}

/* rozwiązanie z pętlą while */
const a = parseInt(prompt('Wprowadź podstawę potęgi!'));
const n = parseInt(prompt('Wprowadź wykładnik potęgi!'));

if (isNaN(a) || isNaN(n)) {
console.error('Wprowadzono nieodpowiedni format danych. Odśwież stronę i spróbuj jeszcze raz!');
} else {
let j = 2;
const powResult = Math.pow(a, n);

let message = `${a}`

if (n === 0) {
message = `${a} do potęgi zerowej to zero!`;
} else if (n === 1) {
message = `${a} do potęgi 1 to ${a}!`;
} else if (n === -1) {
message = `${a} do potęgi 1 to -${a}!`;
} else {
while (j <= n) {
const addon = ` * ${a}`
message = `${message}${addon}`;

/* rozwiązanie z pętlą while */
j++
};
console.log(message + ' = ' + powResult);
};
};
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

15 changes: 12 additions & 3 deletions 02/index.html
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
<!DOCTYPE html>
<html lang="pl">

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<meta
name="viewport"
content="width=device-width, initial-scale=1.0"
>
<meta
http-equiv="X-UA-Compatible"
content="ie=edge"
>
<title>devmentor.pl - JS BASICS - #02</title>
</head>

<body>

<script src="./app.js"></script>
</body>

</html>
48 changes: 43 additions & 5 deletions 03/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,49 @@ const c = randomNumber(min, max);
console.log(a, b, c);






const isEven = function (int) {
if (typeof int === 'number') {
if (int % 2 === 0) {
return true
} else {
return false
}
} else {
return null
}
};

function randomNumber(min, max) {
return Math.round((Math.random() * (max - min)) + min);
}
};

function getSum(a, b, c) {
const aP = parseInt(a);
const bP = parseInt(b);
const cP = parseInt(c);

const min = Math.min(aP, bP, cP);
return aP + bP + cP - min
};

function showInfo(x, y) {
switch (y) {
case null:
console.log(`Podany argument ${x} nie jest liczbą`);
break;
case true:
console.log(`Podany argument ${x} jest parzysty`);
break;
case false:
console.log(`Podany argument ${x} jest nie parzysty`);
break;
default:
console.error('Wprowadzono błędne parametry funkcji showInfo! Parametr "y" przyjmuje wartości: "null", "true" lub "false".')
};
};


const sum = getSum(a, b, c);
const sumIsEven = isEven(sum);

showInfo(sum, sumIsEven);
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

30 changes: 30 additions & 0 deletions 04/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const array = createArray(10, 200);
const top10 = getLargest(array);
console.log(getAvg(top10));




function createArray(min, max) {
const array = [];

for (let i = 0; i < 20; i++) {
const randomNumber = Math.round((Math.random() * (max - min)) + min);
array.push(randomNumber);
};

return array
};

function getLargest(array) {
const newArray = array.sort((a, b) => b - a).slice(0, 10);
return newArray
};

function getAvg(array) {
const avg = array.reduce((inc, curr) => {
return inc += curr
}, 0) / array.length;
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Przydałoby się sprawdzić czy nie dzielimy przez 0 :)


return avg
};
15 changes: 12 additions & 3 deletions 04/index.html
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
<!DOCTYPE html>
<html lang="pl">

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<meta
name="viewport"
content="width=device-width, initial-scale=1.0"
>
<meta
http-equiv="X-UA-Compatible"
content="ie=edge"
>
<title>devmentor.pl - JS BASICS - #04</title>
</head>

<body>

<script src="./app.js"></script>
</body>

</html>
38 changes: 38 additions & 0 deletions 05/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
function Student(firstName, lastName) {
this.firstName = firstName;
this.lastName = lastName;
this.grades = {};
};


Student.prototype.addGrade = function (className, grade) {
if (this.grades[className]) {
this.grades[className].push(grade)
} else (
this.grades[className] = [grade]
)
};

Student.prototype.getAverageGrade = function (className) {

if (!className) {
let avg = 0;

for (const key in this.grades) {
const avgKey = this.grades[key].reduce((inc, curr) => {
return inc += curr
}, 0) / this.grades[key].length;
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Uważamy na dzielenie przez 0 :) Faktycznie mało prawdopodbne, że występuje nazwa przedmiotu bez oceny, ale może jutro czy za miesiąc zostanie dodana metoda o usuwaniu ocen przypadkowo wystawionych i już na pewno nie będziesz pamiętał, aby dodać tutaj warunek dzielenia przez 0 :)

avg += avgKey;
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Musimy pamiętać, że w programowaniu często liczby są przechowywane w systemie dwójkowym, co powoduje, że operacje na wartościach ułamkowych może prowadzić do niedokładności (najczęstszy przykład, wpisz do consoli 0.1+02 (patrz screen). Dlatego lepiej najpierw zsumować oceny i potem podzielić przez ich ilość, a nie robić średnią dla każdego

Image

z osobna.

}

return avg / Object.keys(this.grades).length;
} else if (typeof className === "string") {
const avgSub = this.grades[className].reduce((inc, curr) => {
return inc += curr
}, 0) / this.grades[className].length;

return avgSub
Comment on lines +30 to +34
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Zwróć uwagę, że ta część jest dość podobna do tej z linii 22-25, lepiej by było napisać funkcję, która przyjmuje przez paramtr tablicę z ocenami i liczy średnią. Potem ta funkcja w obu miejscach jest uruchamiana.

} else {
return console.error('Zły format parametru getAverageGrade()')
}
}
15 changes: 12 additions & 3 deletions 05/index.html
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
<!DOCTYPE html>
<html lang="pl">

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<meta
name="viewport"
content="width=device-width, initial-scale=1.0"
>
<meta
http-equiv="X-UA-Compatible"
content="ie=edge"
>
<title>devmentor.pl - JS BASICS - #05</title>
</head>

<body>

<script src="./app.js"></script>
</body>

</html>