Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div id="app" class="container"></div>

<!-- SOCIAL PANEL HTML -->
<div class="social-panel-container">
	<div class="social-panel">
		<p>Created with <i class="fa fa-heart"></i> by
			<a target="_blank" href="https://florin-pop.com">Florin Pop</a></p>
		<button class="close-btn"><i class="fas fa-times"></i></button>
		<h4>Get in touch on</h4>
		<ul>
			<li>
				<a href="https://twitter.com/florinpop1705" target="_blank">
					<i class="fab fa-twitter"></i>
				</a>
			</li>
			<li>
				<a href="https://linkedin.com/in/florinpop17" target="_blank">
					<i class="fab fa-linkedin"></i>
				</a>
			</li>
			<li>
				<a href="https://facebook.com/florinpop17" target="_blank">
					<i class="fab fa-facebook"></i>
				</a>
			</li>
			<li>
				<a href="https://instagram.com/florinpop17" target="_blank">
					<i class="fab fa-instagram"></i>
				</a>
			</li>
		</ul>
	</div>
</div>
<button class="floating-btn">
	Get in Touch
</button>

<div class="floating-text">
	Part of <a href="https://florin-pop.com/blog/2019/09/100-days-100-projects" target="_blank">#100Days100Projects</a>
</div>
              
            
!

CSS

              
                @import url('https://fonts.googleapis.com/css?family=Muli&display=swap');
@import url('https://fonts.googleapis.com/css?family=Big+Shoulders+Display&display=swap');

* {
	box-sizing: border-box;
}

body {
	background-image: url('https://image.freepik.com/free-photo/white-paper-with-grid-line-pattern-background_64749-690.jpg');
	display: flex;
	align-items: center;
	justify-content: center;
	font-family: 'Big Shoulders Display', sans-serif;
	margin: 0 0 50px;
	padding: 20px;
	min-height: 100vh;
}

.container {
	border: 2px solid #000;
	background-color: rgba(255, 255, 255, 0.7);
	font-size: 2em;
	padding: 30px 20px;
	position: relative;
	margin: auto;
	width: 1000px;
	max-width: 100%;
}

h1 {
	text-align: center;
	margin: 20px 0 50px;
}

.text-center {
	text-align: center;
}

input,
.btn {
	padding: 10px 15px;
	font-family: 'Big Shoulders Display', sans-serif;
	font-size: 20px;
}

.btn {
	background-color: #000;
	border: 2px solid #000;
	color: #fff;
}

.row {
	display: flex;
}

.col-1 {
	flex: 2;
}

.col-2 {
	flex: 1;
}

.form {
	display: flex;
	flex-direction: column;
	align-items: center;
	justify-content: center;
}

.calculation {
	display: flex;
	flex-wrap: wrap;
	justify-content: center;
	align-items: center;
}

.calculation .op {
	padding: 10px;
	font-size: 100px;
	line-height: 100px;
	margin: 0;
}

.answer {
	border: 0;
	border: 2px solid #000;
	background: transparent;
	font-family: 'Big Shoulders Display', sans-serif;
	font-size: 100px;
	padding: 10px;
	max-width: 200px;
}

.answer:focus {
	outline: none;
}

.answer.wrong {
	border-color: orangered;
}

.score {
	position: absolute;
	top: 20px;
	left: 20px;
}

.name {
	position: absolute;
	top: 20px;
	right: 20px;
}

.leaderboard {
	border-radius: 5px;
}

h3 {
	margin: 0 0 15px;
	text-decoration: underline;
}

.leaderboard ul {
	padding: 0;
	margin: 0;
	list-style-type: none;
}

.leaderboard ul li {
	display: flex;
	justify-content: space-between;
	margin: 5px 0;
}

@media screen and (max-width: 1000px) {	
	.container {
		width: 100%;	
	}

	.row {
		flex-direction: column;
	}
	
	.answer {
		font-size: 80px;
		max-width: 150px;
	}

	.calculation .op {
		font-size: 80px;
	}

	.leaderboard {
		margin: 40px auto 0;
		max-width: 300px;
	}
}

/* SOCIAL PANEL CSS */
.social-panel-container {
	position: fixed;
	right: 0;
	bottom: 80px;
	transform: translateX(100%);
	transition: transform 0.4s ease-in-out;
}

.social-panel-container.visible {
	transform: translateX(-10px);
}

.social-panel {	
	background-color: #fff;
	border-radius: 16px;
	box-shadow: 0 16px 31px -17px rgba(0,31,97,0.6);
	border: 5px solid #001F61;
	display: flex;
	flex-direction: column;
	justify-content: center;
	align-items: center;
	font-family: 'Muli';
	position: relative;
	height: 169px;	
	width: 370px;
	max-width: calc(100% - 10px);
}

.social-panel button.close-btn {
	border: 0;
	color: #97A5CE;
	cursor: pointer;
	font-size: 20px;
	position: absolute;
	top: 5px;
	right: 5px;
}

.social-panel button.close-btn:focus {
	outline: none;
}

.social-panel p {
	background-color: #001F61;
	border-radius: 0 0 10px 10px;
	color: #fff;
	font-size: 14px;
	line-height: 18px;
	padding: 2px 17px 6px;
	position: absolute;
	top: 0;
	left: 50%;
	margin: 0;
	transform: translateX(-50%);
	text-align: center;
	width: 235px;
}

.social-panel p i {
	margin: 0 5px;
}

.social-panel p a {
	color: #FF7500;
	text-decoration: none;
}

.social-panel h4 {
	margin: 20px 0;
	color: #97A5CE;	
	font-family: 'Muli';	
	font-size: 14px;	
	line-height: 18px;
	text-transform: uppercase;
}

.social-panel ul {
	display: flex;
	list-style-type: none;
	padding: 0;
	margin: 0;
}

.social-panel ul li {
	margin: 0 10px;
}

.social-panel ul li a {
	border: 1px solid #DCE1F2;
	border-radius: 50%;
	color: #001F61;
	font-size: 20px;
	display: flex;
	justify-content: center;
	align-items: center;
	height: 50px;
	width: 50px;
	text-decoration: none;
}

.social-panel ul li a:hover {
	border-color: #FF6A00;
	box-shadow: 0 9px 12px -9px #FF6A00;
}

.floating-btn {
	border-radius: 26.5px;
	background-color: #001F61;
	border: 1px solid #001F61;
	box-shadow: 0 16px 22px -17px #03153B;
	color: #fff;
	cursor: pointer;
	font-size: 16px;
	line-height: 20px;
	padding: 12px 20px;
	position: fixed;
	bottom: 20px;
	right: 20px;
	z-index: 999;
}

.floating-btn:hover {
	background-color: #ffffff;
	color: #001F61;
}

.floating-btn:focus {
	outline: none;
}

.floating-text {
	background-color: #001F61;
	border-radius: 10px 10px 0 0;
	color: #fff;
	font-family: 'Muli';
	padding: 7px 15px;
	position: fixed;
	bottom: 0;
	left: 50%;
	transform: translateX(-50%);
	text-align: center;
	z-index: 998;
}

.floating-text a {
	color: #FF7500;
	text-decoration: none;
}

@media screen and (max-width: 480px) {

	.social-panel-container.visible {
		transform: translateX(0px);
	}
	
	.floating-btn {
		right: 10px;
	}
}
              
            
!

JS

              
                class App extends React.Component {
	state = {
		nr1: undefined,
		nr2: undefined,
		sign: undefined,
		correct_answer: undefined,
		answer: undefined,
		score: 0,
		isWrong: false,
		database: undefined,
		users: [],
		user: undefined
	};

	componentWillMount = () => {
		const database = firebase.database().ref('/users');
		this.setState(
			{
				database
			},
			() => {
				this.getUsers();
				this.createCalculation();
			}
		);
	};

	getUsers = () => {
		const { database } = this.state;

		database.on('value', snapshot => {
			const users = [];
			const usersObj = snapshot.val();
			if (usersObj) {
				Object.keys(usersObj).forEach(key => {
					users.push(usersObj[key]);
				});

				this.setState({
					users
				});
			}
		});
	};

	getRandomNumber = (size = 1) => {
		const minNr = 1; // +'1'.padEnd(size, '0'); <- this for more complex calculations
		const maxNr = +Array(size)
			.fill(9)
			.join('');
		const nr = Math.floor(Math.random() * (maxNr - minNr + 1) + minNr);
		return nr;
	};

	getRandomSign = () => {
		const signs = ['+', '-', '*', '/'];
		const idx = Math.floor(Math.random() * signs.length);
		return signs[idx];
	};

	createCalculation = (size = 1) => {
		const nr1 = this.getRandomNumber(size);
		const nr2 = this.getRandomNumber(size);
		const sign = this.getRandomSign();
		const correct_answer = eval(`${nr1} ${sign} ${nr2}`);

		// Don't allow floating point answers nor Infinity
		if (
			Math.floor(correct_answer) !== correct_answer ||
			correct_answer === Infinity
		) {
			return this.createCalculation(size);
		} else {
			this.setState({
				nr1,
				nr2,
				sign,
				correct_answer
			});
		}
	};

	handleChange = e => {
		this.setState({
			// convert it to number
			answer: +e.target.value
		});
	};

	handleSubmit = e => {
		e.preventDefault();
		const { correct_answer, answer, score } = this.state;

		// increasing/decreasing the score & difficulty based on how far the user goes
		// score increment = how many digits does his score has <- old one
		// score increment = every 20 points
		const score_increment = Math.floor(Math.abs(score) / 25) + 1; // score.toString().split('').length; <- old
		let newScore = undefined;

		if (correct_answer === answer) {
			newScore = score + score_increment;
			e.target.reset();
			this.createCalculation(score_increment);
			this.setState({
				score: newScore,
				isWrong: false
			});
		} else {
			newScore = score - 1;
			this.setState({
				isWrong: true,
				score: newScore
			});
		}

		this.updateScoreInDB(newScore);
	};

	updateScoreInDB = score => {
		const { database, user } = this.state;

		database.child(user.id).update({
			score
		});
	};

	handleNameChange = e => {
		this.setState({
			// convert it to number
			name: e.target.value
		});
	};

	handleNameSubmit = e => {
		const { name, database } = this.state;

		if (!name) return;

		const newUser = {
			score: 0,
			name,
			lastUpdated: Date.now(),
			// generating a random id
			id: Math.random()
				.toString()
				.substr(2)
		};

		database.child(newUser.id).set(newUser);

		this.setState({
			user: newUser
		});
	};

	render() {
		const { nr1, nr2, sign, score, isWrong, user, users } = this.state;

		return (
			<>
				<h1>Math Wizz</h1>
				{user && <small className='name'>Hello, {user.name}</small>}
				{user ? (
					<div className='row'>
						<div className='col-1'>
							<form className='form' onSubmit={this.handleSubmit}>
								<h3>Calculate:</h3>
								<div className='calculation'>
									<p className='op'>{nr1}</p>
									<p className='op'>{sign}</p>
									<p className='op'>{nr2}</p>
									<p className='op'>=</p>
									<input
										type='number'
										className={`answer ${isWrong && 'wrong'}`}
										onChange={this.handleChange}
									/>
								</div>
								<div className='score'>Score: {score}</div>
							</form>
						</div>
						<div className='col-2'>
							<div className='leaderboard'>
								<h3>Leaderboard</h3>
								<ul>
									{users.length > 0 &&
										users
											.sort((a, b) => b.score - a.score)
											.slice(0, 10)
											.map((user, idx) => (
												<li key={idx}>
													<span>{user.name}</span> {user.score}
												</li>
											))}
								</ul>
							</div>
						</div>
					</div>
				) : (
					<div className='text-center'>
						<p>
							Welcome to the Math Wizz game. <br />
							Let's test your math skills! 😃 <br />
							<br />
							You'll be given random math questions that you'll have to answer.
							<br />
							Level changes when you reach the score of: 25, 50, 75...
						</p>
						<p>Please enter your username to start:</p>
						<input type='text' onChange={this.handleNameChange} />
						<button className="btn" onClick={this.handleNameSubmit}>Submit</button>
					</div>
				)}
			</>
		);
	}
}

ReactDOM.render(<App />, document.getElementById('app'));


// SOCIAL PANEL JS
const floating_btn = document.querySelector('.floating-btn');
const close_btn = document.querySelector('.close-btn');
const social_panel_container = document.querySelector('.social-panel-container');

floating_btn.addEventListener('click', () => {
	social_panel_container.classList.toggle('visible')
});

close_btn.addEventListener('click', () => {
	social_panel_container.classList.remove('visible')
});
              
            
!
999px

Console