Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Homework 18 #20

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
101 changes: 101 additions & 0 deletions js-core/homeworks/homework-18/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Home work 18</title>
</head>
<body>
<style type="text/css">
td{
width: 40px;
height: 40px;
border: 1px solid black;
}
td.selected{
background-color: red;
}
</style>

<table>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
</table>

<script src="src/main.js"></script>

<style>
section {
display: flex;
width: 100%;
height: 350px;
flex: 1;
}
div {
flex: 1;
flex-direction: column;
display: flex;
align-items: flex-end;
}
.one {
background-color: coral;
}
.two {
background-color: slateblue;
}
.three {
background-color: fuchsia;
}
.resize {
width: 25px;
height: 100%;
background: black;
}
</style>
<section>
<div class="one">
<div class="resize"></div>
</div>
<div class="two">
<div class="resize"></div>
</div>
<div class="three">
<div class="resize"></div>
</div>
</section>
<script src="src/events.js"></script>

</body>
</html>
31 changes: 31 additions & 0 deletions js-core/homeworks/homework-18/src/events.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

// @SUPER-FrontEnd
/*
У вас есть 3 блока смотри events.html
при mousedown и движении мышки нужно сделать
ресайз одного блока.
Подсказка - событие необходимо повесить на доп. элемент .resize
*/

let section = document.getElementsByTagName('section')[0];
let clickedBlock;
let changeBlockSize = function(event){
let movementDiff = clickedBlock.clickedAt - event.pageX;
let newWidth = clickedBlock.startWidth + movementDiff;
clickedBlock.style.width = newWidth + 'px';
}

section.addEventListener('mousedown', function(event){
if(event.target.classList.contains('resize')){
clickedBlock = event.target;
clickedBlock.clickedAt = event.pageX;
clickedBlock.startWidth = event.target.offsetWidth;
document.body.addEventListener('mousemove', changeBlockSize);
}
});

document.body.addEventListener('mouseup', function(){
document.body.removeEventListener('mousemove', changeBlockSize);
});


113 changes: 113 additions & 0 deletions js-core/homeworks/homework-18/src/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@

/* EASY
Алгоритмы !
TASK 0
Вам дано предложение, верните массив из слов,
которые длинее чем средняя длина всех слов.
Слова разделены пробелами, если в предложении запятые,они должны быть пропущены
solution(["This is a sample string"]) expected ["This" "sample" "string"]
solution(["Some another sample"]) expected ["another" "sample"]
solution(["Do, do, do, do... do it!"]) expected []
*/

function solution(sentence){
// regex is copypasta
let sentenceNoCommas = sentence.replace(/[.,\/#!$%\^&\*;:{}=\-_`~()]/gmi,'');
console.log(sentence);
let sentenceArray = sentenceNoCommas.split(' ');
sentenceArray = sentenceArray.filter( value => value !== '');
let wordsLength = sentenceArray.reduce( (acc,value) => {
return acc + value.length;
},0);
let avgWordLength = wordsLength/sentenceArray.length;
console.log('sentence:', sentence);
console.log('avgWordLength',avgWordLength);
// sentenceArray = sentenceArray.map( value => {
// });
return sentenceArray.filter( value => value.length > avgWordLength);
}
console.log(solution("This, ,is, a, sample , string"));
console.log('-----------------');
console.log(solution("Some another sample"));
console.log('-----------------');

console.log(solution("Do, do, do, do... do it!"));


/*
Подготовка к занятию в пятницу.
Windows:
Установите все node.js скачать отсюда -> https://nodejs.org/en/
Скачайте последнюю 10.x.x версию
Для проверки установки в консоле введите "node -v" (без кавычек) отобразит версию node.js
Linux:
sudo apt install npm // установить менеджер пакетов npm
npm i -g n // установить пакет для установки node.js
sudo n latest // установить последнюю версию node.js
*/


/* TASK 1
Сделайте таблицу 5x5
При клике на элемент, изменяйте цвет у элемента на красный.
Но цвет у элемента должен изменяться пропорционально в другой половине квадрата
Пример 3х3:
[]|[]|[]
[]|[]|[]
[]|[]|[]
кликаем сюда -> []|[]|[]
[]|[]|[]
[]|[]|[x] <- загорается тут
[]|[]|[]
кликаем сюда -> []|[]|[x] <- загорается тут
[]|[]|[x]


*/
const table = document.getElementsByTagName('table')[0];
let getClickedPosition = function(td){
let position = {
x:1,
y:1
}
let xElement = td;
while(xElement.previousElementSibling){
position.x++;
xElement = xElement.previousElementSibling
}
let yElement = td.parentElement;
while(yElement.previousElementSibling){
position.y++;
yElement = yElement.previousElementSibling
}
return position;
}
let reversePosition = function(clickedPosition){
return {
x: (6 - clickedPosition.x),
y: (6 - clickedPosition.y)
}
}
let findTdAtPosition = function(position){
let tr = table.children[0].children[position.y - 1];
let td = tr.children[position.x - 1];
return td;
}
table.addEventListener('click', (event) =>{
if(event.target.nodeName === 'TD'){
let clickedPosition = getClickedPosition(event.target);
let needFirePosition = reversePosition(clickedPosition);
let toFireTd = findTdAtPosition(needFirePosition);
toFireTd.classList.toggle('selected');
}else{
console.error('Not a TD element');
}
});

// @SUPER-FrontEnd
/*
У вас есть 3 блока смотри events.html
при mousedown и движении мышки нужно сделать
ресайз одного блока.
Подсказка - событие необходимо повесить на доп. элемент .resize
*/