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(typeof a);
console.log(typeof b);

const addition = Number(a) + b;
const concatenation = a + b.toString();
const subtraction = a - b;
const multiplication = a * b;
const division = b / a;
const modulo = b % a;

console.log(
`addition: ${addition}, concatenation: ${concatenation}, subtraction: ${subtraction}, multiplication: ${multiplication}, division: ${division}, modulo: ${modulo}`,
);

const arr = [addition, concatenation, subtraction, multiplication, division, modulo];

arr.forEach(el => {
if (Number(el) >= 20) {
console.log(`${el} jest większe od 20`);
} else {
console.log(`${el} jest mniejsze od 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.

👍

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

<body>

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

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

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

if (isNaN(x) || x < 1 || x > 9) {
console.log('Wpisz poprawną liczbę z zakresu 1-9.');
} else {
for (let i = 1; i < 10; i++) {
console.log(`${x} x ${i} = ${x * i}`);
}
}

/* rozwiązanie z pętlą while */

// const a = 3;
// const n = 4;

const a = Number(prompt('Podaj podstawę potęgi 1-9'));
const n = Number(prompt('Podaj wykładnik potęgi 1-9'));

if (isNaN(a) || isNaN(n) || a < 1 || a > 9 || n < 1 || n > 9) {
console.log('Wpisz poprawną liczbę z zakresu 1-9.');
} else {
let i = 1;
let result = 1;
let text = '';

while (i <= n) {
result *= a;

if (i === n) {
text += `${a}`;
} else {
text += `${a} * `;
}

i++;
}

/* rozwiązanie z pętlą while */
console.log(`${text} = ${result}`);
}
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.

👍

5 changes: 4 additions & 1 deletion 02/index.html
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
<!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">
<title>devmentor.pl - JS BASICS - #02</title>
</head>

<body>

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

</html>
46 changes: 39 additions & 7 deletions 03/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,47 @@ const max = 100;
const a = randomNumber(min, max);
const b = randomNumber(min, max);
const c = randomNumber(min, max);
// const a = 21;
// const b = 33;
// const c = 21;

console.log(a, b, c);






function getSum(num1, num2, num3) {
const intNum1 = parseInt(num1);
const intNum2 = parseInt(num2);
const intNum3 = parseInt(num3);

return [intNum1, intNum2, intNum3]
.sort((a, b) => a - b)
.slice(-2)
.reduce((acc, curr) => acc + curr, 0);
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.

Super!

}

const isEven = function (num) {
return typeof num !== 'number' ? null : num % 2 === 0;
};

const showInfo = (val, flag) => {
switch (flag) {
case null:
console.log(`Podany argument ${val} nie jest liczbą`);
break;
case true:
console.log(`Podany argument ${val} jest parzysty`);
break;
case false:
console.log(`Podany argument ${val} jest nieparzysty`);
break;
default:
console.log('Drugi parametr musi być: null, true lub false');
}
};

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

const totalSum = getSum(a, b, c);
const isNumberIsEven = isEven(totalSum);
showInfo(totalSum, isNumberIsEven);
28 changes: 28 additions & 0 deletions 04/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const min = 1;
const max = 100;

const arr = createArray(1, 100);
console.log(arr);

const largest = getLargest(arr);
console.log(largest);

const avg = getAvg(largest);
console.log(avg);

function createArray(min, max) {
const arr = [];
for (let i = 0; i < 20; i++) {
const num = Math.round(Math.random() * (max - min) + min);
arr.push(num);
}
return arr;
}

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

function getAvg(array) {
return array.reduce((acc, curr) => acc + 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.

👍

5 changes: 4 additions & 1 deletion 04/index.html
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
<!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">
<title>devmentor.pl - JS BASICS - #04</title>
</head>

<body>

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

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

Student.prototype.addGrade = function (subject, grade) {
if (!this.grades[subject]) {
this.grades[subject] = [];
}
return this.grades[subject].push(grade);
};

Student.prototype.getAverageGrade = function (subject) {
if (!subject) {
const newArr = [];
for (const sub in this.grades) {
newArr.push(...this.grades[sub]);
}
return this.calculateAverage(newArr);
} else {
if (!this.grades[subject]) {
return 'Nie ma takiego przedmiotu';
} else {
return this.calculateAverage(this.grades[subject]);
}
}
};

Student.prototype.calculateAverage = function (arr) {
return Number((arr.reduce((acc, curr) => acc + curr, 0) / arr.length).toFixed(2));
};
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.

Zdecydowanie lepiej! Teraz jak będzie trzeba obliczać inaczej średnią - np. podawać 3 miejsca po przecinku to nie będzie to żaden problem. Najważniejsze, że taka zmiana będzie dotyczyć tylko jednego miejsca i zawsze staramy się do tego dążyć.


const student = new Student('Jan', 'Kowalski');
student.addGrade('maths', 4);
student.addGrade('maths', 6);
student.addGrade('english', 3);
console.log(student);
const avgMath = student.getAverageGrade('maths');
const avgMath1 = student.getAverageGrade('math');
console.log(avgMath);
console.log(avgMath1);
const avg = student.getAverageGrade();
console.log(avg);
2 changes: 1 addition & 1 deletion 05/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
<title>devmentor.pl - JS BASICS - #05</title>
</head>
<body>

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