-
Notifications
You must be signed in to change notification settings - Fork 0
Hw 6 #6
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
IgoryanM
wants to merge
2
commits into
main
Choose a base branch
from
HW_6
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Hw 6 #6
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| <!DOCTYPE html> | ||
| <html lang="en"> | ||
| <head> | ||
| <meta charset="UTF-8"> | ||
| <meta http-equiv="X-UA-Compatible" content="IE=edge"> | ||
| <meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
| <title>Cart</title> | ||
| <link rel="stylesheet" href="style.css"> | ||
| </head> | ||
| <body> | ||
| <div id='cart'></div> | ||
| <script src="cart.js"></script> | ||
| </body> | ||
| </html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| const cart = { | ||
| goodsInCart: [], | ||
| cartDiv: document.getElementById('cart'), | ||
|
|
||
| init() { | ||
| this.cartDiv.style.backgroundColor = 'orange'; | ||
| this.update(this.cartDiv); | ||
| }, | ||
|
|
||
| countCartPrice() { | ||
| let CartPrice = 0; | ||
| this.goodsInCart.forEach(function(item) { | ||
| CartPrice += item.price * item.quantity; | ||
| }); | ||
| return CartPrice; | ||
| }, | ||
|
|
||
| update() { | ||
| if (this.goodsInCart.length > 0) this.cartDiv.textContent = `В корзине ${this.goodsInCart.length} товара на сумму ${this.countCartPrice()} $.`; | ||
| else this.cartDiv.textContent = `Ваша корзина пуста.`; | ||
| } | ||
|
|
||
| }; | ||
|
|
||
| const catalog = { | ||
| cart, | ||
| goods: [ | ||
| { | ||
| title: 'iphone', | ||
| price: 1000, | ||
| quantity: 1 | ||
| }, | ||
| { | ||
| title: 'imac', | ||
| price: 1500, | ||
| quantity: 1 | ||
| }, | ||
| { | ||
| title: 'ipad', | ||
| price: 1200, | ||
| quantity: 1 | ||
| }], | ||
|
|
||
| init() { | ||
| for (let i in this.goods) { | ||
| const catalogDiv = document.createElement('div'); | ||
| const btn = document.createElement('div'); | ||
|
|
||
| catalogDiv.className = 'item'; | ||
| //catalogDiv.id = this.goods[i].title; | ||
| btn.className = 'button'; | ||
| btn.id = this.goods[i].title; | ||
|
|
||
| document.body.appendChild(catalogDiv); | ||
| document.body.appendChild(btn); | ||
| catalogDiv.textContent = this.goods[i].title + ': ' +this.goods[i].price + ' $'; | ||
| btn.textContent = 'Купить'; | ||
|
|
||
| this.initClickHandler(btn); | ||
| } | ||
| }, | ||
|
|
||
| initClickHandler(elem) { | ||
| elem.addEventListener('click', (event) => this.clickHandler(event)); | ||
| }, | ||
|
|
||
| clickHandler(event) { | ||
| if (event.target.tagName === 'DIV') { | ||
| this.addToCart(event.target.id); | ||
| console.log(this.cart.goodsInCart); | ||
| cart.update(); | ||
| }; | ||
| }, | ||
|
|
||
| addToCart(goodTitle) { | ||
| let goodInCart = false; | ||
|
|
||
| for (let i in this.goods) { | ||
| if (goodTitle == this.goods[i].title) { | ||
| if (cart.goodsInCart.length == 0) cart.goodsInCart.push(this.goods[i]); | ||
| else { | ||
| for (let j in cart.goodsInCart) { | ||
| if (cart.goodsInCart[j].title == goodTitle) { | ||
| cart.goodsInCart[j].quantity += 1; | ||
| goodInCart = true; | ||
| } | ||
| } | ||
| if (goodInCart == false) cart.goodsInCart.push(this.goods[i]); | ||
| }; | ||
| }; | ||
| }; | ||
| } | ||
|
|
||
| } | ||
|
|
||
|
|
||
| catalog.init(); | ||
| cart.init(); | ||
|
Comment on lines
+97
to
+98
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. чтобы не завязываться на внешние данные, лучше передать ссылку на каталог в init каталога и сохранить внутри объекта, чтобы далее весь код опирался именно на this.cart |
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| * { | ||
| margin: 0; | ||
| padding: 0; | ||
| } | ||
|
|
||
| body { | ||
| font-family: sans-serif | ||
| } | ||
|
|
||
|
|
||
| .button { | ||
|
|
||
| box-shadow: 5px 20px 50px rgba(16, 112, 177, 0.2); | ||
| border-radius: 10px; | ||
| font-weight: 500; | ||
| font-size: 16px; | ||
| line-height: 26px; | ||
| text-align: center; | ||
| color: #2beb6b; | ||
| text-transform: uppercase; | ||
| background-color: rgb(68, 9, 146); | ||
|
|
||
| } | ||
|
|
||
|
|
||
|
|
||
| .item { | ||
| margin: 10px auto; | ||
| padding: 20px 10px; | ||
| background-color: lightblue; | ||
| border: solid; | ||
| border-color: #8dc3f3; | ||
| border-width: 3px; | ||
| } | ||
|
|
||
| .item:hover { | ||
| background-color: rgb(76, 197, 238); | ||
| } | ||
|
|
||
| .container_top{ | ||
| display: flex; | ||
| flex-direction: column; | ||
| } | ||
|
|
||
| .menu { | ||
| display: flex; | ||
| justify-content: space-between; | ||
| list-style: none; | ||
| padding-top: 34px; | ||
| } | ||
|
|
||
|
|
||
| .button_top{ | ||
| font-size: 16px; | ||
| line-height: 26px; | ||
| text-align: center; | ||
| letter-spacing: 0.04em; | ||
| color: #FFFFFF; | ||
| background: #5A98D0; | ||
| box-shadow: 5px 10px 20px rgba(53, 110, 173, 0.2); | ||
| border-radius: 10px; | ||
| padding: 15px 50px; | ||
| display: inline-block; | ||
| text-decoration: none; | ||
| margin-top: 71px; | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
дороговатое решение, посмотрите в сторону метода find у массива