Skip to content

homework_17 #21

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

Open
wants to merge 1 commit 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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added js-core/homeworks/homework-17/bestsportcar170.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
20 changes: 20 additions & 0 deletions js-core/homeworks/homework-17/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<!DOCTYPE html>
<html>
<head>
<title>homework_17</title>
<meta charset="utf-8" />
<link href='style.css' rel='stylesheet' type='text/css'>
</head>
<body>
<div>
<img src="bestsportcar170.jpg">
</div>
<div class="buttons">
<button class ="previous"> <= </button>
<span> 1 </span>
<button class ="next"> => </button>
</div>
<div class="fetch"></div>
<script src="index.js"></script>
</body>
</html>
117 changes: 117 additions & 0 deletions js-core/homeworks/homework-17/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
// TASK 0
const solution = arr => {
let newArr = [];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For at all, we could look at what we need from our array? We have to reduce its size but leave the same arguments.
So the first step will be to use .filter method.

A solution can look that way

  1. Take the element
  2. Take the element-index
  3. Take leaved part of an array to element-index
  4. Check if some elemnt in array is bigger than current element ( please note, there the same JavaScript method called .some
  5. if no one from that elements is bigger than current, leave it in array otherwise drop it

So I guess it could be solved with

.filter + .some from arrays method

let lastElem = arr[arr.length - 1];
arr.pop();
let tmp = function(arr) {
let index;
let leader = arr[0];
for(let i = 0; i < arr.length; i++) {
if(arr[i] >= leader) {
leader = arr[i];
index = i;
};
};
newArr.push(leader);
arr.splice(0, (index + 1));
if(arr.length > 1) {
tmp(arr);
};
};
tmp(arr);
newArr.push(lastElem);
return newArr;
};

console.log(solution([16, 17, 4, 3, 5, 2])); // === [17, 5, 2]
console.log(solution([4, 3, 7, 12, 6, 67, 5, 45, 34, 35, 2, 8])); // [67, 45, 35, 8]
console.log(solution([12, 10, 12, 8, 7, 6])); // [12, 8, 7, 6]
console.log(solution([1, 2, 3, 4, 5, 4])); // [5, 4]
console.log(solution([12, 12, 12])); // [12, 12]


//TASK1
class Carousel {
constructor() {
this.arrImg = ["bestsportcar170.jpg", "Acura-NSX-2016-2017-02.jpg", "ferrari_667_171011.jpg"];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably Carousel should not be worrying about picture names.

Our Carousel should only be responsible to know about

  • active element
  • changing elements left/right

That's all. And we have to apply it in several places in every app :) without any knowledge of pictures

this.count = 0;
this.img = document.querySelector("img");
this.nextButton = document.querySelector("button.next");
this.previousButton = document.querySelector("button.previous");
this.numberImg = document.querySelector("span");
};

onclickNext() {
let pointer = this;
pointer.nextButton.onclick = function() {
if(pointer.count === (pointer.arrImg.length - 1)) {
pointer.count = -1;
}
pointer.img.src = pointer.arrImg[++pointer.count];
pointer.numberImg.textContent = pointer.count + 1;
};
};

onclickPrevious() {
let pointer = this;
pointer.previousButton.onclick = function() {
if(pointer.count <= 0) {
pointer.count = pointer.arrImg.length;
};
pointer.img.src = pointer.arrImg[--pointer.count];
pointer.numberImg.textContent = pointer.count + 1;
};
};

buttonOperation() {
this.onclickNext();
this.onclickPrevious();
}
};
let carousel = new Carousel();
carousel.buttonOperation();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that call seems weird - because it looks like the internals of Carousel.
As a developer who uses your carousel, I don't want to worry about buttonOperation. I want just apply your carousel and it works "out of the box"




//Task2
class AddStyle {
topStyle(selector, objCssStyle) {
let resultCssStr = `<style> .${selector} { `;
let styleArr = Object.keys(objCssStyle);
let propertiesCssString = styleArr.reduce((done, elem) => {
return done + `${convertingCssProperties(elem)}: ${objCssStyle[elem]};\n`;
}, '');
resultCssStr += propertiesCssString + '</style>';
let head = document.head.innerHTML;
document.head.innerHTML += resultCssStr;
};
};

let addStyle = new AddStyle();
addStyle.topStyle('fetch', {backgroundColor:'blue'});


function convertingCssProperties(str) {
let regExp = /[A-Z]/;
let index = str.search(regExp);
let substring = function(str) {
if(index !== -1) {
return `-${str[index].toLowerCase()}`;
};
};
let convertingStr = str.replace(regExp, substring(str));
return convertingStr;
};



//Super
function liquidCss(str) {
let regExp = /-[a-z]/;
let index = str.search(regExp);
let liquidCss = str.replace(regExp, str[index + 1].toUpperCase());
return liquidCss;
};
console.log(liquidCss('background-color'));
console.log(liquidCss('margin-left'));
console.log(liquidCss('flex-basis'));
14 changes: 14 additions & 0 deletions js-core/homeworks/homework-17/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
img {
width: 800px;
height: 400px;
}

.buttons {
margin-left: 320px;
}

.fetch {
width: 100px;
height: 100px;
border: 2px solid black;
}