feat: Add randomization toggle and emoji buttons to BinarySearchTrick
- Add toggle switch to control randomized vs ordered numbers - Replace 'Present' and 'Absent' buttons with ✅ and ❌ emojis - Fix copy link functionality by adding proper URL parameter - Improve user experience with better visual indicators
This commit is contained in:
parent
e636df7fd1
commit
e591bed158
1 changed files with 31 additions and 7 deletions
|
|
@ -16,6 +16,7 @@ const BinarySearchTrick = ({ showSocialShare = true }: BinarySearchTrickProps) =
|
||||||
const [result, setResult] = useState<number | null>(null);
|
const [result, setResult] = useState<number | null>(null);
|
||||||
const [showResult, setShowResult] = useState(false);
|
const [showResult, setShowResult] = useState(false);
|
||||||
const [isOneByOne, setIsOneByOne] = useState(false);
|
const [isOneByOne, setIsOneByOne] = useState(false);
|
||||||
|
const [isRandomized, setIsRandomized] = useState(true);
|
||||||
const [currentCardIndex, setCurrentCardIndex] = useState(0);
|
const [currentCardIndex, setCurrentCardIndex] = useState(0);
|
||||||
const [cards, setCards] = useState<Array<{id: number; title: string; numbers: number[]}>>([]);
|
const [cards, setCards] = useState<Array<{id: number; title: string; numbers: number[]}>>([]);
|
||||||
|
|
||||||
|
|
@ -29,7 +30,7 @@ const BinarySearchTrick = ({ showSocialShare = true }: BinarySearchTrickProps) =
|
||||||
return shuffled;
|
return shuffled;
|
||||||
};
|
};
|
||||||
|
|
||||||
// Generate cards with randomized numbers but maintaining binary logic
|
// Generate cards with optional randomization
|
||||||
const generateCards = () => {
|
const generateCards = () => {
|
||||||
const baseCards = [
|
const baseCards = [
|
||||||
{
|
{
|
||||||
|
|
@ -59,17 +60,17 @@ const BinarySearchTrick = ({ showSocialShare = true }: BinarySearchTrickProps) =
|
||||||
}
|
}
|
||||||
];
|
];
|
||||||
|
|
||||||
// Randomize the order of numbers within each card
|
// Apply randomization based on toggle state
|
||||||
return baseCards.map(card => ({
|
return baseCards.map(card => ({
|
||||||
...card,
|
...card,
|
||||||
numbers: shuffleArray(card.numbers)
|
numbers: isRandomized ? shuffleArray(card.numbers) : card.numbers
|
||||||
}));
|
}));
|
||||||
};
|
};
|
||||||
|
|
||||||
// Initialize cards on component mount
|
// Initialize cards on component mount
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
setCards(generateCards());
|
setCards(generateCards());
|
||||||
}, []);
|
}, [isRandomized]);
|
||||||
|
|
||||||
// Handle mode toggle
|
// Handle mode toggle
|
||||||
const handleModeToggle = (checked: boolean) => {
|
const handleModeToggle = (checked: boolean) => {
|
||||||
|
|
@ -79,6 +80,14 @@ const BinarySearchTrick = ({ showSocialShare = true }: BinarySearchTrickProps) =
|
||||||
setShowResult(false);
|
setShowResult(false);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Handle randomization toggle
|
||||||
|
const handleRandomizationToggle = (checked: boolean) => {
|
||||||
|
setIsRandomized(checked);
|
||||||
|
setCardStates({});
|
||||||
|
setShowResult(false);
|
||||||
|
setCurrentCardIndex(0);
|
||||||
|
};
|
||||||
|
|
||||||
// Navigation functions for one-by-one mode
|
// Navigation functions for one-by-one mode
|
||||||
const goToPreviousCard = () => {
|
const goToPreviousCard = () => {
|
||||||
setCurrentCardIndex(prev => Math.max(0, prev - 1));
|
setCurrentCardIndex(prev => Math.max(0, prev - 1));
|
||||||
|
|
@ -109,7 +118,7 @@ const BinarySearchTrick = ({ showSocialShare = true }: BinarySearchTrickProps) =
|
||||||
setResult(null);
|
setResult(null);
|
||||||
setShowResult(false);
|
setShowResult(false);
|
||||||
setCurrentCardIndex(0);
|
setCurrentCardIndex(0);
|
||||||
setCards(generateCards()); // Regenerate with new randomized order
|
setCards(generateCards()); // Regenerate with current randomization setting
|
||||||
};
|
};
|
||||||
|
|
||||||
// Check if at least one card has been selected (present or absent)
|
// Check if at least one card has been selected (present or absent)
|
||||||
|
|
@ -201,6 +210,20 @@ const BinarySearchTrick = ({ showSocialShare = true }: BinarySearchTrickProps) =
|
||||||
One by one
|
One by one
|
||||||
</Label>
|
</Label>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
<div className="flex items-center justify-center gap-3 pt-4">
|
||||||
|
<Label htmlFor="randomization-toggle" className="text-sm font-medium">
|
||||||
|
Randomize numbers
|
||||||
|
</Label>
|
||||||
|
<Switch
|
||||||
|
id="randomization-toggle"
|
||||||
|
checked={isRandomized}
|
||||||
|
onCheckedChange={handleRandomizationToggle}
|
||||||
|
/>
|
||||||
|
<Label htmlFor="randomization-toggle" className="text-sm font-medium">
|
||||||
|
Ordered numbers
|
||||||
|
</Label>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</CardContent>
|
</CardContent>
|
||||||
</Card>
|
</Card>
|
||||||
|
|
@ -259,14 +282,14 @@ const BinarySearchTrick = ({ showSocialShare = true }: BinarySearchTrickProps) =
|
||||||
variant={cardState === 'present' ? 'default' : 'outline'}
|
variant={cardState === 'present' ? 'default' : 'outline'}
|
||||||
onClick={() => handleCardSelection(card.id, 'present')}
|
onClick={() => handleCardSelection(card.id, 'present')}
|
||||||
>
|
>
|
||||||
Present
|
✅
|
||||||
</Button>
|
</Button>
|
||||||
<Button
|
<Button
|
||||||
size="sm"
|
size="sm"
|
||||||
variant={cardState === 'absent' ? 'destructive' : 'outline'}
|
variant={cardState === 'absent' ? 'destructive' : 'outline'}
|
||||||
onClick={() => handleCardSelection(card.id, 'absent')}
|
onClick={() => handleCardSelection(card.id, 'absent')}
|
||||||
>
|
>
|
||||||
Absent
|
❌
|
||||||
</Button>
|
</Button>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
@ -326,6 +349,7 @@ const BinarySearchTrick = ({ showSocialShare = true }: BinarySearchTrickProps) =
|
||||||
<SocialShare
|
<SocialShare
|
||||||
title="Binary Search Magic Trick"
|
title="Binary Search Magic Trick"
|
||||||
description="Learn how binary numbers work through this amazing magic trick! Can you figure out how it works?"
|
description="Learn how binary numbers work through this amazing magic trick! Can you figure out how it works?"
|
||||||
|
url={`${window.location.origin}/binary-search-trick`}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue