Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added public/images/dolphin.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/images/lobster.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/images/ocean.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/images/starfish.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
51 changes: 51 additions & 0 deletions src/components/animalFacts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { animals } from '../js/animals'
import React from 'react';

const title = '';
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

didn't have to use constant.
so these are unused and then background is img tag.

const showBackground = true;
const background = (
<img
className="background"
alt="ocean"
src="/images/ocean.jpg"
/>
);
const images = [];

for (const animal in animals) {
images.push(
<img
key={animal}
className="animal"
alt={animal}
src={animals[animal].image}
aria-label={animal}
role="button"
onClick={displayFact}
/>
);
}
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you could use map instead of a for...in loop.
and then key must be string or number.


function displayFact(e) {
const animalName = e.target.alt;
const facts = animals[animalName].facts;
const randomIndex = Math.floor(Math.random() * facts.length);
const funFact = facts[randomIndex];

const factElement = document.getElementById('fact');
factElement.innerHTML = funFact;
}

const animalFacts = (
<div>
<h1>{title || 'Click an animal for a fun fact'}</h1>
{showBackground && background}
<div className="animals">
{images}
</div>
<p id="fact"></p>
</div>
);


export default animalFacts;
63 changes: 63 additions & 0 deletions src/css/styles.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
div {
display: flex;
align-items: center;
flex-direction: column;
}

.background {
position: absolute;
margin-top: 80px;
width: 95%;
z-index: 1
}

#fact {
display: flex;
justify-content: center;
position: fixed;
margin-top: 100px;
width: 90%;
font-size: 3vw;
z-index: 2;
}

.animals {
display: flex;
flex-direction: row;
position: fixed;
width: 85%;
margin-top: 240px;
z-index: 2
}

.animal {
width: 33.3%;
}

@media only screen and (max-width: 670px) {
.animals {
margin-top: 180px;
}

h1 {
font-size: 24px;
}
}

@media only screen and (max-width: 390px) {
.animals {
margin-top: 150px;
}
}

@media only screen and (max-width: 300px) {
.animals {
margin-top: 130px;
}
}

@media only screen and (max-width: 200px) {
.animals {
margin-top: 120px;
}
}
7 changes: 4 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import React from 'react';
import ReactDOM from 'react-dom/client';
import './index.css';
import App from './App';
import './css/styles.css';
import animalFacts from './components/animalFacts';
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

component name must be Upper case.

import reportWebVitals from './reportWebVitals';

const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(
<React.StrictMode>
<App />
{animalFacts}
</React.StrictMode>
);

// If you want to start measuring performance in your app, pass a function
// to log results (for example: reportWebVitals(console.log))
// or send to an analytics endpoint. Learn more: https://bit.ly/CRA-vitals
reportWebVitals();
reportWebVitals();
14 changes: 14 additions & 0 deletions src/js/animals.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
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!']
}
};