Skip to content

Commit

Permalink
edit
Browse files Browse the repository at this point in the history
  • Loading branch information
Asmaa ElBanna committed Aug 5, 2024
1 parent b948cf6 commit d8b5a7d
Showing 1 changed file with 4 additions and 11 deletions.
15 changes: 4 additions & 11 deletions script.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,23 +78,19 @@ const runTickerAnimation = () => {
};

const selectPrize = () => {
// Calculate total weight
const totalWeight = prizes.reduce((acc, prize) => acc + prize.weight, 0);

// Generate a random number between 0 and total weight
let random = Math.random() * totalWeight;

console.log(`Random value for selection: ${random}`);

// Find the prize based on the random number and weight
const selectedPrize = prizes.find(({ weight }) => (random -= weight) < 0);

if (!selectedPrize) {
console.error('No prize selected');
return;
}

// Get the index of the selected prize
const selectedIndex = prizes.indexOf(selectedPrize);

console.log(`Selected prize: ${selectedPrize.text}, Index: ${selectedIndex}`);
Expand Down Expand Up @@ -122,7 +118,6 @@ trigger.addEventListener("click", () => {
trigger.disabled = true;
prizeNodes.forEach((prize) => prize.classList.remove(selectedClass));

// Select prize but don't display yet
const { prize, index } = selectPrize() || {};
if (!prize) return;

Expand All @@ -131,7 +126,7 @@ trigger.addEventListener("click", () => {
const randomOffset = Math.floor(Math.random() * rotationPerSlice);
const targetRotation = index * rotationPerSlice + randomOffset;

rotation = 360 * 5 + targetRotation; // Spin the wheel 5 times, then land on the target slice
rotation = 360 * 5 + targetRotation;
wheel.classList.add(spinClass);
spinner.style.setProperty("--rotate", rotation);
ticker.style.animation = "none";
Expand All @@ -146,12 +141,11 @@ spinner.addEventListener("transitionend", () => {
wheel.classList.remove(spinClass);
spinner.style.setProperty("--rotate", rotation);

// Display the prize after the wheel stops spinning
if (selectedPrize) {
const selectedIndex = prizes.findIndex(prize => prize.text === selectedPrize.text);
prizeNodes[selectedIndex].classList.add(selectedClass); // Add class to highlight selected prize
prizeNodes[selectedIndex].classList.add(selectedClass);
displayPrize(selectedPrize);
selectedPrize = null; // Reset selectedPrize
selectedPrize = null;
}

resetSlider();
Expand All @@ -167,7 +161,6 @@ const startSpinFromSlider = () => {
trigger.disabled = true;
prizeNodes.forEach((prize) => prize.classList.remove(selectedClass));

// Select prize but don't display yet
const { prize, index } = selectPrize() || {};
if (!prize) return;

Expand All @@ -176,7 +169,7 @@ const startSpinFromSlider = () => {
const randomOffset = Math.floor(Math.random() * rotationPerSlice);
const targetRotation = index * rotationPerSlice + randomOffset;

rotation = 360 * 5 + targetRotation; // Spin the wheel 5 times, then land on the target slice
rotation = 360 * 5 + targetRotation;
wheel.classList.add(spinClass);
spinner.style.setProperty("--rotate", rotation);
ticker.style.animation = "none";
Expand Down

0 comments on commit d8b5a7d

Please sign in to comment.