Advertisement
maycod23

Untitled

Sep 27th, 2023
23
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.95 KB | None | 0 0
  1. // src/components/FlashCardDeck.tsx
  2. import React, { useState } from 'react';
  3. import FlashCard from './FlashCard';
  4. import { FlashCard as FlashCardType } from '../types/FlashCard';
  5.  
  6. type FlashCardDeckProps = {
  7. flashcards: FlashCardType[];
  8. };
  9.  
  10. const FlashCardDeck: React.FC<FlashCardDeckProps> = ({ flashcards }) => {
  11. const [shuffledFlashcards, setShuffledFlashcards] = useState(flashcards);
  12.  
  13. const shuffleFlashcards = () => {
  14. const shuffled = [...shuffledFlashcards];
  15. for (let i = shuffled.length - 1; i > 0; i--) {
  16. const j = Math.floor(Math.random() * (i + 1));
  17. [shuffled[i], shuffled[j]] = [shuffled[j], shuffled[i]];
  18. }
  19. setShuffledFlashcards(shuffled);
  20. };
  21.  
  22. return (
  23. <div>
  24. <button onClick={shuffleFlashcards}>Shuffle</button>
  25. {shuffledFlashcards.map((flashcard) => (
  26. <FlashCard key={flashcard.id} flashCard={flashcard} />
  27. ))}
  28. </div>
  29. );
  30. };
  31.  
  32. export default FlashCardDeck;
  33.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement