Skip to content

Instantly share code, notes, and snippets.

@ahojukka5
Last active January 2, 2020 21:39
Show Gist options
  • Save ahojukka5/a72677ead295a418624c448cf62bc1db to your computer and use it in GitHub Desktop.
Save ahojukka5/a72677ead295a418624c448cf62bc1db to your computer and use it in GitHub Desktop.
hy-fullstack-2019-part-1-anecdotes
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<link rel="icon" href="%PUBLIC_URL%/favicon.ico" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="theme-color" content="#000000" />
<meta
name="description"
content="Web site created using create-react-app"
/>
<link rel="apple-touch-icon" href="logo192.png" />
<!--
manifest.json provides metadata used when your web app is installed on a
user's mobile device or desktop. See https://developers.google.com/web/fundamentals/web-app-manifest/
-->
<link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
<!--
Notice the use of %PUBLIC_URL% in the tags above.
It will be replaced with the URL of the `public` folder during the build.
Only files inside the `public` folder can be referenced from the HTML.
Unlike "/favicon.ico" or "favicon.ico", "%PUBLIC_URL%/favicon.ico" will
work correctly both with client-side routing and a non-root public URL.
Learn how to configure a non-root public URL by running `npm run build`.
-->
<title>React App</title>
</head>
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
<div id="root"></div>
<!--
This HTML file is a template.
If you open it directly in the browser, you will see an empty page.
You can add webfonts, meta tags, or analytics to this file.
The build step will place the bundled scripts into the <body> tag.
To begin the development, run `npm start` or `yarn start`.
To create a production bundle, use `npm run build` or `yarn build`.
-->
</body>
</html>
import React, { useState } from "react";
import ReactDOM from "react-dom";
const Button = ({ onClick, text }) => <button onClick={onClick}>{text}</button>;
const App = props => {
const [selected, setSelected] = useState(0);
const [votes, setVotes] = useState({});
const onNext = () => {
setSelected(Math.floor(Math.random() * props.anecdotes.length));
};
const onVote = () => {
const copy = { ...votes };
copy[selected] = (copy[selected] || 0) + 1;
setVotes(copy);
};
let maxVoteIndex = -1;
let maxVote = 0;
Object.keys(votes).forEach(key => {
if (votes[key] > maxVote) {
maxVoteIndex = key;
maxVote = votes[key];
}
});
console.log("votes", votes, "maxVoteIndex ", maxVoteIndex);
return (
<div>
<h1>Anecdote of the day</h1>
<p>{props.anecdotes[selected]}</p>
<p>has {votes[selected] || 0} votes</p>
<Button onClick={onVote} text="vote" />
<Button onClick={onNext} text="next anecdote" />
<h1>Anecdote with most votes ({maxVote} votes)</h1>
<p>{props.anecdotes[maxVoteIndex]}</p>
</div>
);
};
const anecdotes = [
"If it hurts, do it more often",
"Adding manpower to a late software project makes it later!",
"The first 90 percent of the code accounts for the first 90 percent of the development time...The remaining 10 percent of the code accounts for the other 90 percent of the development time.",
"Any fool can write code that a computer can understand. Good programmers write code that humans can understand.",
"Premature optimization is the root of all evil.",
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it."
];
ReactDOM.render(<App anecdotes={anecdotes} />, document.getElementById("root"));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment