HTML Academy
Got it in stock? What if I find one?
Introduction to JavaScript in the browser20/23
Back to the list of tasks
  • 1. Script to start!
  • 2. Ready or not, here I come
  • 3. First class
  • 4. Find every single one
  • 5. Under cover
  • 6. Temporarily unavailable
  • 7. Special offer
  • 8. Summary of “JavaScript in the browser”. Part 1
  • 9. Twelfth program: “Ice cream test”
  • 10. Be careful, children!
  • 11. Creating a card
  • 12. Describe yourself
  • 13. Don’t repeat yourself
  • 14. Refactor and conquer
  • 15. Adding a picture
  • 16. One more function
  • 17. Check yourself
  • 18. Live data
  • 19. Got it in stock? What if I find one?
  • 20. Special offer
  • 21. Start the conveyor
  • 22. Summary of “JavaScript in the browser”. Part 2
  • 23. Thirteenth program: “Ice cream returns”
Start the conveyor
  • Sign up
  • Log in

Loading…
Everything will be ready in few seconds

  • Theory
  • Theory
  • Comments

Special offer

All we have left to do is process the last two properties from the upload: isSpecial and specialPrice. The isSpecial property contains true if the product is covered by the special offer. The special-offer product gets a new price, which is stored in specialPrice.

You have already solved a similar task: special-offer cards should get an additional class product--special. But now you also need to show a special price. It should be in a paragraph with class product__special-price. This paragraph should be the last child element in the card.

The order of actions is as follows:

  1. check whether the product is covered by special offer;
  2. if the check goes through, add a class to the product card,
  3. and also add another paragraph with the corresponding class and text from the specialPrice property into the card.

After we are done, let’s test the function once again. In the data object, change the value of the isSpecial to true and specialPrice to 300 (value null means “nothing” or, if applied closer to our real-life scenario, “there are no special prices”). If everything is done correctly, the appearance of the card will change.

Why did the price change from 200 to 300? Because this way the skillful marketer Muffin can interpret the special offer.

Comments

  • index.html
  • style.css
  • script.js
HTML
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Catalog | Device</title> <link rel="stylesheet" href="device/setting.css"> <link rel="stylesheet" href="style.css"> </head> <body> <ul class="products"> </ul> <script src="script.js"></script> </body> </html>
CSS
.products { display: flex; flex-wrap: wrap; justify-content: space-between; width: 570px; margin: 20px auto; padding: 0; list-style: none; } .product { position: relative; display: flex; flex-wrap: wrap; justify-content: space-between; width: 270px; margin-bottom: 30px; } .product__image { order: -1; width: 270px; height: 380px; margin-bottom: 15px; } .product__title { max-width: 180px; margin: 0; font-weight: 500; font-size: 18px; line-height: 24px; } .product__price { max-width: 80px; margin: 0; text-align: right; font-weight: 300; font-size: 16px; line-height: 27px; } .product--special { order: -1; width: 100%; height: 380px; flex-direction: column; justify-content: flex-start; align-content: space-between; } .product--special .product__title { width: 270px; max-width: 100%; margin-bottom: 10px; font-size: 28px; line-height: 36px; } .product--special .product__price { width: 270px; max-width: 100%; text-align: left; text-decoration: line-through; color: #cccccc; } .product--special .product__special-price { margin: 0; } .product--special .product__image { outline: 3px solid #ffc600; } .product--special::before { content: "Product of the day"; position: absolute; top: 0; left: 0; display: flex; justify-content: center; align-items: center; padding: 5px 10px; font-weight: 500; color: #111111; text-transform: uppercase; background-color: #ffc600; } .product--available::after, .product--unavailable::after { position: absolute; top: 343px; left: 50%; padding: 10px 5px 2px 5px; border-bottom: 1px solid #57c74b; transform: translateX(-50%); } .product--available::after { content: "In stock"; } .product--unavailable { filter: grayscale(1) opacity(0.7); } .product--unavailable::after { content: "Out of stock"; } .product--special.product--available::after, .product--special.product--unavailable::after { left: 135px; }
JavaScript
var makeElement = function (tagName, className, text) { var element = document.createElement(tagName); element.classList.add(className); if (text) { element.textContent = text; } return element; }; var createCard = function (product) { var listItem = makeElement('li', 'product'); var title = makeElement('h2', 'product__title', product.text); listItem.appendChild(title); var picture = makeElement('img', 'product__image'); picture.src = product.imgUrl; picture.alt = product.text; listItem.appendChild(picture); var price = makeElement('p', 'product__price', product.price); listItem.appendChild(price); var availabilityClass = 'product--available'; if (!product.isAvailable) { availabilityClass = 'product--unavailable'; } listItem.classList.add(availabilityClass); return listItem; }; var cardList = document.querySelector('.products'); var productInfo = { isAvailable: true, imgUrl: 'device/item-1.jpg', text: 'Selfie stick for beginners', price: 200, isSpecial: false, specialPrice: null }; var cardItem = createCard(productInfo); cardList.appendChild(cardItem);

What didn’t you like in this task?

Thanks! We’ll fix everything at once!

Click inside the mini browser to put the focus in this window.

100%
Console
Goalscompleted
0
    1. In the createCard function before return, add a check to make sure that the product is covered by the special offer.
    2. If the condition is met, add the product--special class to the product card.
    3. Below in the condition, create a variable specialPrice and write in it a paragraph with the price of the special offer created with makeElement.
    4. Below in the condition, add element specialPrice to the end of listItem.
    5. In the productInfo object, replace isSpecial with true and the value of specialPrice with 300.

    Cookies ∙ Privacy ∙ License Agreement ∙ About ∙ Contacts ∙ © HTML Academy OÜ, 2019−2025

    VISAMastercard

    Log in

    or

    Forgot your password?

    Sign up

    Sign up

    or
    Log in

    Restore access

    Have you forgotten your password or lost access to your profile? Enter your email connected to your profile and we will send you a link to restore access.

    Forgot to connect your email to the profile? Email us and we’ll help.