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
31 changes: 29 additions & 2 deletions 01/assets/js/app.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,31 @@
const a = '4.2';
const a = "4.2";
const b = 9;

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

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

const operations = ["+", "-", "*", "/", "%"];

const results = operations.map((operation) => {
switch (operation) {
case "+":
return Number(a) + Number(b);
case "-":
return Number(a) - Number(b);
case "*":
return Number(a) * Number(b);
case "/":
return Number(a) / Number(b);
case "%":
return Number(a) % Number(b);
}
});

results.forEach((result, index) => {
const operation = operations.at(index);
console.log(
`${a} ${operation} ${b} is ${result > 20 ? "greater" : "less"} than 20`
);
});
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.

👍

18 changes: 9 additions & 9 deletions 01/index.html
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<!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">
<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" />
<title>devmentor.pl - JS BASICS - #01</title>
</head>
<body>

</body>
</html>
</head>
<body>
<script src="./assets/js/app.js"></script>
</body>
</html>
12 changes: 6 additions & 6 deletions 02/README.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
> :white_check_mark: *Jeśli będziesz mieć problem z rozwiązaniem tego zadania, poproś o pomoc na odpowiednim kanale na Slacku, tj. `s1e04-js-basics` (dotyczy [mentee](https://devmentor.pl/mentoring-javascript/)) lub na ogólnodostępnej i bezpłatnej [społeczności na Discordzie](https://devmentor.pl/discord). Pamiętaj, aby treść Twojego wpisu spełniała [odpowiednie kryteria](https://devmentor.pl/jak-prosic-o-pomoc/).*
> :white_check_mark: _Jeśli będziesz mieć problem z rozwiązaniem tego zadania, poproś o pomoc na odpowiednim kanale na Slacku, tj. `s1e04-js-basics` (dotyczy [mentee](https://devmentor.pl/mentoring-javascript/)) lub na ogólnodostępnej i bezpłatnej [społeczności na Discordzie](https://devmentor.pl/discord). Pamiętaj, aby treść Twojego wpisu spełniała [odpowiednie kryteria](https://devmentor.pl/jak-prosic-o-pomoc/)._

&nbsp;

# `#02` JavaScript: Podstawy


Tym razem wykorzystasz dwa rodzaje pętli.

## Pętla for

Twój kolega ma syna w podstawówce i właśnie uczy go tabliczki mnożenia. Zaoferowałeś pomoc w formie programu, który pokaże wszystkie działania mnożenia dla wybranej liczby.

Za pomocą pętli `for` stwórz rozwiązanie, które będzie wyświetlać kolejne wyniki mnożenia.

- Pierwszym czynnikiem niech będzie wartość zmiennej `x`, np. `const x = 4`.
- W miejsce drugiego czynnika wstaw kolejne cyfry od 1 do 9.

Expand All @@ -31,14 +31,14 @@ Zwróć uwagę, że najpierw tworzymy rozwiązanie dla konkretnego przypadku, a

## Pętla while

Pomyślałeś, że przy okazji stworzysz program do nauki potęgowania.
Pomyślałeś, że przy okazji stworzysz program do nauki potęgowania.

Niech działa on następująco: jeśli `a = 3` (podstawa) oraz `n = 4` (wykładnik) to w konsoli wyświetla się `3 * 3 * 3 * 3 = 81`

W tym rozwiązaniu również możesz użyć `prompt()` i sprawdzać, czy użytkownik wprowadza odpowiednie dane.


&nbsp;
> :no_entry: *Jeśli nie posiadasz materiałów do tego zadania tj. **PDF + wideo, projekt + Code Review**, znajdziesz je na stronie [devmentor.pl](https://devmentor.pl/workshop-js-basics/)*

> :arrow_left: [*poprzednie zadanie*](./../01) | [*następne zadanie*](./../03) :arrow_right:
> :no_entry: _Jeśli nie posiadasz materiałów do tego zadania tj. **PDF + wideo, projekt + Code Review**, znajdziesz je na stronie [devmentor.pl](https://devmentor.pl/workshop-js-basics/)_

> :arrow_left: [_poprzednie zadanie_](./../01) | [_następne zadanie_](./../03) :arrow_right:
35 changes: 32 additions & 3 deletions 02/app.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,36 @@

/* rozwiązanie z pętlą for */
const x = 4;
const x = prompt("Type number: ");

if (x && !isNaN(x)) {
for (let i = 1; i <= 9; i++) {
console.log(`${x} x ${i} = ${Number(x) * i}`);
}
} else {
console.error("Invalid input");
}

/* rozwiązanie z pętlą while */
let a;
let n;

while (!a || isNaN(a)) {
a = Number(prompt("Podstawa: "));
}

while (!n || isNaN(n) || n <= 1) {
n = Number(prompt("Wykladnik:"));
}

let counter = 0;
let text = "";

while (counter < n) {
text += a;

if (counter !== n - 1) text += " * ";
else text = text + " = " + Math.pow(a, n);

counter++;
}

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.

👍

/* rozwiązanie z pętlą while */
console.log(text);
18 changes: 9 additions & 9 deletions 02/index.html
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<!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">
<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" />
<title>devmentor.pl - JS BASICS - #02</title>
</head>
<body>

</body>
</html>
</head>
<body>
<script src="./app.js"></script>
</body>
</html>
44 changes: 36 additions & 8 deletions 03/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,40 @@ const c = randomNumber(min, max);

console.log(a, b, c);







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

function getSum(x, y, z) {
const numbers = [x, y, z]
.map((number) => parseInt(number))
.sort((a, b) => b - a);

return numbers.at(0) + numbers.at(1);
}

const isEven = function (number) {
if (isNaN(number)) return null;

return number % 2 === 0;
};

function showInfo(arg1, arg2) {
if (!(typeof arg2 === "boolean" || arg2 === null)) return;

switch (arg2) {
case null:
console.log(`Podany argument ${arg1} nie jest liczbą`);
case true:
console.log(`Podany argument ${arg1} jest parzysty`);
break;
case false:
console.log(`Podany argument ${arg1} jest nieparzysty`);
break;
}
}

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

showInfo(sum, even);
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.

👍

38 changes: 38 additions & 0 deletions 04/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
function createArray(min, max) {
if (
typeof min !== "number" ||
typeof max !== "number" ||
isNaN(min) ||
isNaN(max)
) {
throw new Error("min and max must be valid numbers.");
}

if (min > max) {
throw new Error("min must be less than or equal to max.");
}
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.

👍


const array = [];

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

return array;
}

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

function getAvg(array) {
const sum = array.reduce((acc, cur) => acc + cur, 0);
return sum / array.length;
}

const arr = createArray(10, 200);
const largest = getLargest(arr);
const avg = getAvg(largest);

console.log(avg);
18 changes: 9 additions & 9 deletions 04/index.html
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<!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">
<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" />
<title>devmentor.pl - JS BASICS - #04</title>
</head>
<body>

</body>
</html>
</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 (subject, grade) {
if (typeof this.grades[subject] === "undefined") {
this.grades[subject] = [];
}

this.grades[subject].push(grade);
};

Student.prototype.getAverageGrade = function (subject) {
const calcAverage = (grades) => {
if (!grades || grades.length === 0) throw new Error("No grades found!");
return grades.reduce((acc, cur) => acc + cur, 0) / grades.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.

👍


if (typeof subject === "undefined") {
const grades = Object.values(this.grades).flat();
return Number(calcAverage(grades).toFixed(2));
}

return calcAverage(this.grades[subject]);
};

const student = new Student("Jan", "Kowalski");
student.addGrade("maths", 4);
student.addGrade("maths", 6);
student.addGrade("english", 3);
const avgMath = student.getAverageGrade("maths");
const avg = student.getAverageGrade();

console.log(student);
console.log(avgMath);
console.log(avg);
18 changes: 9 additions & 9 deletions 05/index.html
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<!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">
<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" />
<title>devmentor.pl - JS BASICS - #05</title>
</head>
<body>

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