-
-
Save codecademydev/a39d17bb32b5c8f1891ff248c3ea9cdb to your computer and use it in GitHub Desktop.
Codecademy export
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export const animals = { | |
dolphin: { | |
image: '/images/dolphin.jpg', | |
facts: ['Dolphins have been shown to give distinct names to each other!', 'Dolphins are known to display their own culture!', 'Dolphins have two stomachs!'] | |
}, | |
lobster: { | |
image: '/images/lobster.jpg', | |
facts: ['Lobsters taste with their legs!', 'Lobsters chew with their stomachs!', 'Lobsters can live as long as 100 years.'] | |
}, | |
starfish: { | |
image: '/images/starfish.jpg', | |
facts: ['Starfish can have up to 40 arms!', 'Starfish have no brain and no blood!', 'Starfish can regenerate their own arms!'] | |
} | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { animals } from './animals.js'; | |
import React from 'react'; | |
import ReactDOM from 'react-dom'; | |
const title = ""; | |
const background = | |
<img | |
className="background" | |
alt="ocean" | |
src="/images/ocean.jpg" />; | |
function displayFact(e) { | |
const selectedAnimal = e.target.alt; | |
const animalInfo = animals[selectedAnimal]; | |
const optionIndex = Math.floor(Math.random()*animalInfo.facts.length); | |
const funFact = animalInfo.facts[optionIndex]; | |
document.getElementById('fact').innerHTML = funFact; | |
} | |
const images = []; | |
for(const animal in animals) { | |
images.push( | |
<img | |
key={animal} | |
className='animal' | |
alt='animal' | |
src={animals[animal].image} | |
ariaLabel={animal} | |
role='button' | |
onClick={displayFact} | |
/> | |
) | |
} | |
const animalFacts = ( | |
<div> | |
<h1>{title === "" ? `Click an animal for a fun fact!`: title}</h1> | |
{background} | |
<p id='fact'></p> | |
<div className='animals'> | |
{images} | |
</div> | |
</div> | |
); | |
ReactDOM.render( | |
animalFacts, | |
document.getElementById('root') | |
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="utf-8" /> | |
<link rel="stylesheet" href="./styles.css" /> | |
</head> | |
<body> | |
<div id="root"></div> | |
<script src="https://content.codecademy.com/courses/React/react-course-bundle.min.js"></script> | |
<script src="/app.compiled.js"></script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment