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
23 changes: 22 additions & 1 deletion 01/assets/js/app.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,25 @@
const a = '4.2';
const b = 9;
console.log(a, b);
console.log(typeof a, typeof b);
const add = parseFloat(a) + b;
console.log(add);

console.log(a, b);
const sub = a - b;
console.log(sub);

const multiply = a * b;
console.log(multiply.toFixed(2));

const divided = a / b;
console.log(divided.toFixed(2));

const arr = [add, sub, multiply, divided];

arr.forEach(function (num) {
if (num > 20) {
console.log(`${num.toFixed(2)} jest większe od 20`);
} else {
console.log(`${num.toFixed(2)} jest mniejsze lub równe 20`);
}
});
2 changes: 1 addition & 1 deletion 01/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
<title>devmentor.pl - JS BASICS - #01</title>
</head>
<body>

<script src="./assets/js/app.js"></script>
</body>
</html>
34 changes: 30 additions & 4 deletions 02/app.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,33 @@
const x = Number(prompt('Podaj liczbę!'));

/* rozwiązanie z pętlą for */
const x = 4;
if (x >= 1 && x < 10) {
for (let i = 1; i <= 9; i++) {
res = i * x;
console.log(`${i} x ${x} = ${res}`);
}
} else {
console.log('Podałeś błędne dane!');
}

/* rozwiązanie z pętlą while */
const a = Number(prompt('Podaj liczbę z przedziału 1-100!'));
const n = Number(prompt('Podaj potęgę od 0-10!'));


/* rozwiązanie z pętlą while */
if (a > 1 && a < 100 && n > 0 && n <= 10) {
let i = 0;
let result = 1;
let info = '';
while (i < n) {
result *= a;
if (i > 0) {
info += ' * ';
}
info += a;
i++;
}
info = info + ' = ' + result;
console.log(result);
console.log(info);
} else {
console.log('Podałeś błędne dane!');
}
2 changes: 1 addition & 1 deletion 02/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
<title>devmentor.pl - JS BASICS - #02</title>
</head>
<body>

<script src="./app.js"></script>
</body>
</html>
57 changes: 41 additions & 16 deletions 03/app.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,43 @@
const min = 1;
const max = 100;

const a = randomNumber(min, max);
const b = randomNumber(min, max);
const c = randomNumber(min, max);

console.log(a, b, c);







function getSum(a, b, c) {
const aInt = parseInt(a);
const bInt = parseInt(b);
const cInt = parseInt(c);

const arr = [aInt, bInt, cInt];
arr.sort(function (a, b) {
return b - a;
});
return arr[0] + arr[1];
}

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

console.log(isEven(2), isEven(1), isEven('r'));

function showInfo(value, isEven) {
switch (isEven) {
case null:
console.log(`${value} nie jest liczbą`);

break;
case true:
console.log(`Liczba ${value} jest parzysta`);

break;
case false:
console.log(`Liczba ${value} jest nieparzysta`);
break;
}
}

const sum = getSum(a, b, c);
const even = isEven(sum);
showInfo(sum, even);
function randomNumber(min, max) {
return Math.round((Math.random() * (max - min)) + min);
return Math.round(Math.random() * (max - min) + min);
}
32 changes: 32 additions & 0 deletions 04/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
const arr = createArray(10, 200);
const largest = getLargest(arr);
const avg = getAvg(largest);

console.log(arr, largest, 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(arr) {
arr.sort(function (a, b) {
return b - a;
});
return arr.slice(0, 10);
}

function getAvg(arr) {
if (arr.length === 0) {
return 0;
}
const sum = arr.reduce(function (acc, curr) {
return acc + curr;
}, 0);
return sum / arr.length;
}
2 changes: 1 addition & 1 deletion 04/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
<title>devmentor.pl - JS BASICS - #04</title>
</head>
<body>

<script src="./app.js"></script>
</body>
</html>
50 changes: 50 additions & 0 deletions 05/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
function Student(fName, lName) {
this.firstName = fName;
this.lastName = lName;
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) {
if (typeof subject === 'undefined') {
const grades = [];
for (const grade in this.grades) {
const arr = this.grades[grade];
arr.forEach(function (item) {
grades.push(item);
});
}

return this.avg(grades);
}
if (typeof this.grades[subject] === 'undefined') {
return 0;
}
const grades = this.grades[subject];
return this.avg(grades);
};

Student.prototype.avg = function (arr) {
if (arr.length === 0) {
return 0;
}
const sum = arr.reduce(function (acc, curr) {
return acc + curr;
}, 0);
return sum / arr.length;
};
const student = new Student('Jan', 'Kowalski');
student.addGrade('math', 4);
student.addGrade('math', 2);
student.addGrade('german', 1);
student.addGrade('german', 3);
student.addGrade('german', 2);
student.addGrade('german', 5);
console.log(student.getAverageGrade('math'));
console.log(student.getAverageGrade('german'));
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>