Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
chocolate/app.js
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
57 lines (49 sloc)
2.07 KB
This file contains 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
$(document).ready(function() { | |
// Create chocolate cards | |
function createChocolateCards() { | |
chocolates.forEach((chocolate, index) => { | |
const card = $('<div>') | |
.addClass('chocolate-card') | |
.attr('id', `card-${index}`); | |
card.html(` | |
<h3>${chocolate.name}</h3> | |
<img src="${chocolate.image}" alt="${chocolate.name}"> | |
<div class="details"> | |
<p><strong>Calories:</strong> ${chocolate.calories}</p> | |
<p><strong>Description:</strong> ${chocolate.description}</p> | |
<p><strong>Ingredients:</strong> ${chocolate.ingredients}</p> | |
</div> | |
`); | |
$('#chocolateGrid').append(card); | |
}); | |
} | |
// Toggle details functionality | |
function setupToggleDetails() { | |
$('#toggleDetails').click(function() { | |
$('.details').slideToggle(); | |
$(this).text(function(i, text) { | |
return text === "Show Calories and Content" | |
? "Hide Calories and Content" | |
: "Show Calories and Content"; | |
}); | |
}); | |
} | |
// Random sample functionality | |
function setupRandomSample() { | |
$('#randomSample').click(function() { | |
// Reset all cards to cream background | |
$('.chocolate-card').css('background-color', 'cream'); | |
// Select random card | |
const randomIndex = Math.floor(Math.random() * chocolates.length); | |
const randomCode = Math.floor(Math.random() * 50) + 1; | |
// Highlight selected card | |
$(`#card-${randomIndex}`).css('background-color', 'yellow'); | |
// Show alert with code | |
alert(`Your free sample code is: ${randomCode}\nShow this code to the cashier to receive your free ${chocolates[randomIndex].name} chocolate!`); | |
}); | |
} | |
// Initialize all functionality | |
createChocolateCards(); | |
setupToggleDetails(); | |
setupRandomSample(); | |
}); |