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

console.log(a, b);
const y = parseFloat(a);

console.log('a = ' + typeof(a));
console.log('b = ' + typeof(b));
console.log('y = ' + typeof(y));

console.log(a, b);

const res1 = b + y;
const res2 = a - b;
const res3 = a * b;
const res4 = a / b;
const res5 = b % y;
let res6 = 1; res6++;
let res7 = 2; res7--;
const res8 = a + b;

console.log('konkatenacja: ' + res8);
console.log('Dodawanie: ' + res1);
console.log('Odejmowanie: ' + res2);
console.log('Mnożenie: ' + res3);
console.log('Dzielenie: ' + res4);
console.log('Reszta z dzielenia: ' + res5);
console.log('Inkrementacja: ' + res6);
console.log('Dekrementacja: ' + res7);


function checkResult(name, value) {
if (value > 20) {
console.log(name + value + ' jest większy od 20');
} else {
console.log(name + value + ' jest mniejszy od 20');
}

}

checkResult('Dodawanie', res1);
checkResult('Odejmowanie', res2);
checkResult('Mnożenie ', res3);
checkResult('Dzielenie', res4);
checkResult('Reszta z dzielenia', res5);
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.

👍

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>
30 changes: 28 additions & 2 deletions 02/app.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,33 @@

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


for(i=1; i<10; i++){
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.

Uwaga na słowo kluczowe let tj. for( let i=0, jeśli tego nie napiszesz to tworzymy zmienną globalną i co jest złą praktyką.

console.log(x + 'x' + i + '=' + i * x);
}

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

const a = parseInt(prompt('Podaj podstawe'));
const n = parseInt(prompt('Podaj wykładnik'));


if(isNaN(a) || isNaN(n) || n < 0 ){
console.log('podaj poprawne liczby');
}else {
let result = 1;
let expression = "";
let j = 0;

while (j < n) {
result *= a;
expression += a;
if (j < n - 1) {
expression += " * ";
}
j++;
}
console.log(expression + " = " + 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.

👍


/* rozwiązanie z pętlą while */
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>
44 changes: 40 additions & 4 deletions 03/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,46 @@ const a = randomNumber(min, max);
const b = randomNumber(min, max);
const c = randomNumber(min, max);

console.log(a, b, c);



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

showInfo(result, even)



function getSum(a, b, c){
a = parseInt(a);
b = parseInt(b);
c = parseInt(c);

const sorted = [a, b, c].sort((x, y) => y - x);

return sorted[0] + sorted[1];
}

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


function showInfo(value, flag) {
switch (flag) {
case null:
console.log(`Podany argument ${value} nie jest liczbą`);
break;
case true:
console.log(`Podany argument ${value} jest parzysty`);
break;
case false:
console.log(`Podany argument ${value} jest nieparzysty`);
break;
default:
console.log("Nieprawidłowa wartość drugiego parametru");
}
}
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.

👍




Expand Down
33 changes: 33 additions & 0 deletions 04/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@



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


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

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


const arr = createArray(10, 200);
console.log( 'Tabela ', arr);

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

const avg = getAvg(largest);
console.log("Średnia arytmetyczna ", avg);
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.

👍

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>
51 changes: 51 additions & 0 deletions 05/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
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) {
if (typeof subject !== 'undefined') {

const grades = this.grades[subject];
if (!grades || grades.length === 0) return 0;

const sum = grades.reduce((acc, g) => acc + g, 0);
return (sum / grades.length).toFixed(2);
Comment on lines +20 to +23
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 zaznaczona część jest dość podobna do tej z lini 30-33. Można spróbować napisać metodę i uruchomić w obu miejscach - wtedy nie będzie duplikacji kodu. Traktuj to jako zadanie dodatkowe, które nie jest konieczne.

} else {

let allGrades = [];
for (const key in this.grades) {
allGrades = allGrades.concat(this.grades[key]);
}
if (allGrades.length === 0) return 0;

const sum = allGrades.reduce((acc, g) => acc + g, 0);
return (sum / allGrades.length).toFixed(2);
}
};

Student.prototype.getFullName = function() {
return `${this.firstName} ${this.lastName}`;
};


const student = new Student('Jan', 'Kowalski');

student.addGrade('maths', 4);
student.addGrade('maths', 6);
student.addGrade('english', 3);


console.log(student.getFullName());
console.log(student.getAverageGrade('maths'));
console.log(student.getAverageGrade());
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>