Professional Documents
Culture Documents
Lab 3
Lab 3
Хід роботи:
Завдання 1:
1.1. Розмістіть на сторінці div-блок з ідентифікатором “paragraphs”.
Розмістіть у ньому 5 абзаців з довільним текстом. Сформуйте масив, який
міститиме 5 елементів - кількість символів, які входять до відповідного абзацу
тексту.
Для пошуку абзаців використовуйте метод querySelectorAll та відповідний CSS-
селектор. Для доступу до тексту, який входить в абзац використовуйте
властивість innerText для DOM-елемента. Виведіть у консоль отриманий масив.
Лістинг програми:
Index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- <link rel="Stylesheet" href="task1.css"> -->
<title>Document</title>
</head>
<body>
<div id="paragraphs">
<p>Lorem ipsum dolor sit, amet consectetur adipisicing elit.</p>
<p>Lorem ipsum dolor sit, amet consectetur adipisicing elit. word</p>
<p>Lorem ipsum dolor sit, amet consectetur adipisicing elit. 2 word</p>
<p>Lorem ipsum dolor sit, amet consectetur adipisicing elit. 2 word and
word</p>
</body>
<script src="task1.js"></script>
</html>
script.js
// 1.1
const ps = document.querySelectorAll("p");
console.log(ps)
for(let el of ps){
p_elements.push(el.innerText.length);
}
console.log(p_elements)
// 1.2
let arr = [20, 17, 4, -4, 10, -9, 13, 4, 12, 22, 13, 19, 1, 3];
console.log("Input array = " + arr);
console.log("Maximum = " + Math.max(...arr) + " and Minimum = " + Math.min(...arr));
let even = 0, odd = 0;
for(let num of arr)
if(num % 2 == 0)
even++;
else
odd++;
let arr = [20, 17, 4, -4, 10, -9, 13, 4, 12, 22, 13, 19, 1, 3];
Завдання 2.
Розмістіть на сторінці 3 div-блоки з класами “table”. У кожному з них розмістіть
табличку. У першому блоці - табличку розміром 3х3, другому - 4х4, третьому -
5х5.
В CSS-файлі розмістіть стиль для класу “selected”, який передбачає жовтий фон.
За допомогою querySelectorAll знайдіть div-блоки з класом “table”. Для кожного
знайденого блоку у циклі знайдіть всі вкладені у нього теги td. Для кожного
другого тега td додайте клас “selected”.
Лістинг програми:
.table{
border-collapse: collapse;
margin: 20px;
}
.table td{
width: 30px;
height: 30px;
border: 2px solid black;
}
Лістинг програми:
const matrix = [
[1,4,2,7,-3,0,5],
[5.5,4,0,8,1,1,1],
[3,2,1,0,-1,-2,-3],
[8,0,5,4,0,2,7],
[1,1,2,3,4,1,1]
];
console.log(matrix)
// task 1
const count_positive = (matrix) => matrix.reduce((sum, row) => sum + row.filter((el)
=> el > 0).length, 0);
console.log("Number of positive numbers in matrix = " + count_positive);
// task 2
const count_rows_without_zero = (matrix) => matrix.filter((row) => row.includes(0) ==
false).length;
console.log("Number of rows that do not contain zero element = " +
count_rows_without_zero(matrix))
// task 3
const count_cols_with_zeros = (matrix) =>{
let ans = 0;
let n = matrix.lenght, m = matrix[0].lenght;
for(let j = 0; j < m; j++){
// check for each columns, does it includes at least one zeros
founded = false;
if(founded){
console.log("Founded in " + j + " col");
ans++;
}
}
}
return row;
}
console.log("")
console.log("Product of rows that do not contain negative numbers: ")
const mxN = matrix.length;
const mxM = matrix[0].length;
for(let i = 0; i < mxN; i++){
const finded = matrix[i].find((num) => num < 0) != undefined;
if(!finded)
console.log("Product of " + i + "-th row = " + matrix[i].reduce((pr, num) =>
pr * num, 1));
else
console.log(i + "-th row contain negative numbers")
}
console.log("")
console.log("Sum of columns that do not contain negative numbers: ")
for(let j = 0; j < mxM; j++){
let contains_negative = false;
let sum = 0;
for(let i = 0; i < mxN; i++){
if(matrix[i][j] < 0)
contains_negative = true;
sum += matrix[i][j];
}
if(contains_negative)
console.log("")
console.log("Sum of columns that contain at least one negative number: ")
for(let j = 0; j < mxM; j++){
let contains_negative = false;
let sum = 0;
for(let i = 0; i < mxN; i++){
if(matrix[i][j] < 0)
contains_negative = true;
sum += matrix[i][j];
}
if(contains_negative == false)
console.log(j + "-th columns do not contain negative elements")
else
console.log("Sum of " + j + "-th column = " + sum);
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">