🌐
50 React Coding Puzzles
  • 50 React Coding Puzzles
  • Contact
  • Reviews
  • Table of Contents
  • Introduction
  • 1 - Simple Counter App
  • 2 - Dynamic List of Items with Strikethrough
  • 3 - Color Picker with Hex Code Display
  • 4 - Password Strength Checker
  • 5 - Countdown Timer with Audio
  • 6 - Dynamic Movie List with Filtering and Sorting
  • 7 - Text Animation Component (Fade/Scroll)
  • 8 - Dynamically Generated Text Art
  • 9 - Meme Generator with Custom Text
  • 10 - "What Should I Eat?" Random Recipe Generator
  • 11 - Simple Quiz App with Multiple Choice
  • 12 - Unit Converter
  • 13 - Dynamic Image Gallery with Lightbox
  • 14 - Weather Widget with Temperature and Description
  • 15 - Contact Form with Email and Message Validation
  • 16 - Color Palette Generator
  • 17 - Drag-and-Drop Feature for Rearranging Items
  • 18 - Interactive Quiz with Dynamic Results and Feedback
  • 19 - To-Do List with Task Categorization
  • 20 - Note-Taking App with Markdown Support
  • 21 - Simple Calculator with Basic Operations
  • 22 - Word Scramble Game with Timer and Score
  • 23 - Number Guessing Game
  • 24 - Digital Clock with Time Zone Support
  • 25 - Interactive Time Zone Converter
  • 26 - Weather Forecast App with Location Detection
  • 27 - Real-Time Stock Ticker with Updates
  • 28 - Virtual Dice Roller with Multiple Dice
  • 29 - Responsive Navigation Menu with Dropdowns
  • 30 - Progress Bar for Loading or Task Completion
  • 31 - Modal Window for Alerts and Confirmations
  • 32 - Infinite Scroll for Loading More Content
  • 33 - Form Validation with Error Messages
  • 34 - Search Bar with Filtering and Suggestions
  • 35 - Drag-and-Drop File Upload
  • 36 - Interactive Color Picker with Sliders
  • 37 - Image Carousel with Autoplay
  • 38 - Rating System with Stars or Thumbs Up/Down
  • 39 - Comment Section with Nested Replies
  • 40 - Pagination for Long Lists of Data
  • 41 - Stopwatch app that tracks elapsed time
  • 42 - Responsive E-commerce Product Grid
  • 43 - Random Movie/Book Recommender
  • 44 - Kanban board for managing tasks with drag-and-drop
  • 45 - Chat application with real-time messaging using WebSockets
  • 46 - Calendar that displays events and allows scheduling
  • 47 - Tic-Tac-Toe Game
  • 48 - Hangman Game
  • 49 - Notes App
  • 50 - Expense Tracker
  • Afterword
  • Appendix
Powered by GitBook
On this page
  • Description
  • Algorithm
  • Classes
  • Code
  • Explanation
  • Possible Future Enhancements

43 - Random Movie/Book Recommender

Description

Create a random movie/book recommender that displays a list of recommended movies or books with titles, genres, and ratings, offers a way for users to filter recommendations based on genre preferences, allows users to save favorites for later viewing or reading, and integrates with external APIs to fetch movie or book data.

Algorithm

  1. Create a React component for the recommender

  2. Define the data structure for movies/books (title, genre, rating, etc.)

  3. Use an external API (e.g. TMDB for movies, Goodreads for books) to fetch data

  4. Create a function to generate random recommendations

  5. Create a filter function to filter recommendations by genre

  6. Create a favorite-saving function to save user favorites

  7. Render the list of recommendations with filter and favorite options

Classes

  • Recommender: The main recommender component

  • Recommendation: A single recommendation component (movie or book)

  • Filter: The filter component for genre selection

  • Favorite: The favorite component for saving favorites

Code

Recommender.js

import React, { useState, useEffect } from 'react';
import Recommendation from './Recommendation';
import Filter from './Filter';
import Favorite from './Favorite';

const Recommender = () => {
  const [recommendations, setRecommendations] = useState([]);
  const [genreFilter, setGenreFilter] = useState('');
  const [favorites, setFavorites] = useState([]);

  useEffect(() => {
    fetch('<url-to-fetch>')
      .then(response => response.json())
      .then(data => setRecommendations(data.results));
  }, []);

  const generateRandomRecommendations = () => {
    const randomRecommendations = [];
    for (let i = 0; i < 5; i++) {
      const randomIndex = Math.floor(Math.random() * recommendations.length);
      randomRecommendations.push(recommendations[randomIndex]);
    }
    return randomRecommendations;
  };

  const filterRecommendations = (genre) => {
    setGenreFilter(genre);
    const filteredRecommendations = recommendations.filter((recommendation) => recommendation.genre === genre);
    return filteredRecommendations;
  };

  const saveFavorite = (recommendation) => {
    setFavorites([...favorites, recommendation]);
  };

  return (
    <div>
      <Filter onFilterChange={filterRecommendations} />
      <ul>
        {generateRandomRecommendations().map((recommendation) => (
          <Recommendation
            key={recommendation.id}
            recommendation={recommendation}
            onSaveFavorite={saveFavorite}
          />
        ))}
      </ul>
      <Favorite favorites={favorites} />
    </div>
  );
};

export default Recommender;

Recommendation.js

import React from 'react';

const Recommendation = ({ recommendation, onSaveFavorite }) => {
  return (
    <li>
      <h3>{recommendation.title}</h3>
      <p>{recommendation.genre}</p>
      <p>{recommendation.rating}</p>
      <button onClick={() => onSaveFavorite(recommendation)}>Save Favorite</button>
    </li>
  );
};

export default Recommendation;

Filter.js

import React from 'react';

const Filter = ({ onFilterChange }) => {
  const [genre, setGenre] = useState('');

  const handleFilterChange = (event) => {
    setGenre(event.target.value);
    onFilterChange(genre);
  };

  return (
    <select value={genre} onChange={handleFilterChange}>
      <option value="">All Genres</option>
      <option value="action">Action</option>
      <option value="comedy">Comedy</option>
      <option value="drama">Drama</option>
    </select>
  );
};

export default Filter;

Favorite.js

import React from 'react';

const Favorite = ({ favorites }) => {
  return (
    <ul>
      {favorites.map((favorite) => (
        <li key={favorite.id}>{favorite.title}</li>
      ))}
    </ul>
  );
};

export default Favorite;

Explanation

The code creates a random movie/book recommender that displays a list of recommended movies or books with titles, genres, and ratings, offers a way for users to filter recommendations based on genre preferences, allows users to save favorites for later viewing or reading, and integrates with external APIs to fetch movie or book data. The component uses the useState hook to manage the recommendations, genre filter, and favorites.

Possible Future Enhancements

  • Add support for user authentication and personalized recommendations

  • Add support for more advanced filtering options (e.g. rating, release date)

  • Add support for movie/book reviews and ratings

Previous42 - Responsive E-commerce Product GridNext44 - Kanban board for managing tasks with drag-and-drop

Last updated 10 months ago