tangway / flag-guessing-game-frontend

0 stars 0 forks source link

create rudimentary version of multiple choice interface #1

Open tangway opened 6 months ago

tangway commented 6 months ago
tangway commented 6 months ago

Encountering problem where I dont know how to dynamically show a flag if I choose to use the current data structure i have in questions.js

const questions = [
  {
    answer: 'Saint Lucia',
    choices: ['Belize', 'Singapore', 'Dominica', 'Saint Lucia'],
  },
  {
    answer: 'Sri Lanka',
    choices: ['Madagascar', 'Comoros', 'Guyana', 'Sri Lanka'],
  },
];

export default questions;
tangway commented 6 months ago

Encountering problem where I dont know how to dynamically show a flag if I choose to use the current data structure i have in questions.js

const questions = [
  {
    answer: 'Saint Lucia',
    choices: ['Belize', 'Singapore', 'Dominica', 'Saint Lucia'],
  },
  {
    answer: 'Sri Lanka',
    choices: ['Madagascar', 'Comoros', 'Guyana', 'Sri Lanka'],
  },
];

export default questions;

fixed this by creating a mapOfFlags that references the react components of the flags:

import StLuciaFlag from './StLuciaFlag';
import SriLankaFlag from './SriLankaFlag';

const mapOfFlags = {
  'Saint Lucia': StLuciaFlag,
  'Sri Lanka': SriLankaFlag,
};

export default mapOfFlags;

and changing App.jsx to:

import questions from './questions';
import mapOfFlags from './components/mapOfFlags';

const App = () => {
  const FlagComponent = mapOfFlags[questions[1].answer];

  return (
    <>
      <div className="flag-area">
        <FlagComponent />
      </div>
      <div className="choices">
        <ul>
          {questions[1].choices.map((c, index) => <li key={index}>{c}</li>)}
        </ul>
      </div>
    </>
  );
};

export default App;