Skip to content
Merged
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
8 changes: 8 additions & 0 deletions src/data/content.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import meme from "../assets/activities/meme.jpg"
import numberblocs from "../assets/numberblocks.png"
import wordleicon from "../assets/games/Wordle/wordlejpg.png"
import flagger from "../assets/games/flag guess/flagger.png"
import Calculator from "../pages/activities/Calculator"

export const activities = [
{
Expand Down Expand Up @@ -48,6 +49,13 @@ export const activities = [
icon: "https://www.troublefreepool.com/media/joke-png.127455/full",
urlTerm: "random-jokes",
element: <RandomJoke />
},
{
title: "Calculator",
description: "A Simple Calculator",
icon: "https://cdn2.iconfinder.com/data/icons/ios7-inspired-mac-icon-set/512/Calculator_512.png",
urlTerm: "calculator",
element: <Calculator />
}
];

Expand Down
191 changes: 191 additions & 0 deletions src/pages/activities/Calculator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
import React from 'react';
import "../../styles/pages/activities/calculator.css";

class Calculator extends React.Component {
constructor(props) {
super(props);
this.state = {
screenValue1: 0,
}
this.screenUpdate = this.screenUpdate.bind(this)
}
screenUpdate(a) {
let r = '';
let arr = [];
let op = [];
let temp = this.state.screenValue1.toString();
if(a === "=") {
console.log(temp)
for (let j = 0; j < temp.length; j++) {
if(temp[j] !== '/' && temp[j] !== 'x' && temp[j] !== '-' && temp[j] !== '+') {
r = `${r}${temp[j]}`
}
else {
op.push(temp[j])
arr.push(r)
r = ''
}
}
console.log(arr)
arr.push(r)
if(arr[0] === '' && op[0] === '-') {
arr[1]=`${op[0]}${arr[1]}`
op.shift();
arr.shift();
}
if(arr[0] === '') {
arr[0] = 0
}
console.log(arr)
r = '';
for(let x1 = 0; x1 < arr.length; x1++) {
arr[x1] = parseFloat(arr[x1])
}
console.log(arr)
console.log(op)
for(let x3 = 0; x3 < arr.length; x3++) {
if(isNaN(arr[x3])) {
arr[x3+1] = -arr[x3+1]
arr.splice(x3, 1)
op.splice(x3, 1)
}
}
if(op.length === 0){
return
}
for(let b = 0; b < op.length; b++) {
if(op[b] === '/') {r = arr[0] / arr[1]; arr.shift(); arr[0] = r}
if(op[b] === 'x') {r = arr[0] * arr[1]; arr.shift(); arr[0] = r}
if(op[b] === '-') {r = arr[0] - arr[1]; arr.shift(); arr[0] = r}
if(op[b] === '+') {r = arr[0] + arr[1]; arr.shift(); arr[0] = r}
console.log(r)
}
console.log(r)
if(this.state.screenValue1[0] === 0) {
console.log('doin something funny')
}
r = r.toString()
while(r.length > 15) {
r = r.slice(0,-1)
}
this.setState ({
screenValue1: r
})
return
}
if(a === 'clear') {
this.setState ({
screenValue1: 0
})
return
}
if(this.state.screenValue1.length === 15 || this.state.screenValue1 === 'Digit Limit Met!') {
this.setState (state =>({
screenValue1: 'Digit Limit Met!'
}))
return
}
if(a === '/' || a === 'x' || a === '-' || a === '+') {
let x = this.state.screenValue1[this.state.screenValue1.length-1];
if((a === '-' && x === '/') || (a === '-' && x === 'x') || (a === '-' && x === '+')) {
x = this.state.screenValue1;
this.setState ({
screenValue1: `${x}${a}`
})
return;
}
if(x === '/' || x === 'x' || x === '-' || x === '+') {
x = this.state.screenValue1;
while(x[x.length-1] === '/' || x[x.length-1] === 'x' || x[x.length-1] === '-' || x[x.length-1] === '+') {
x = x.slice(0,-1);
}
this.setState ({
screenValue1: x
})
}
}
if(a === 0 && this.state.screenValue1 === 0) {
this.setState ({
screenValue1: 0
})
}
else {
if(this.state.screenValue1 === 0 && a !== '.') {
this.setState ({
screenValue1: a
})
return
}
if(a === '.' && temp.includes(".") === false) {
this.setState (state =>({
screenValue1: `${state.screenValue1}${a}`
}))
return
}
else if(a === '.') {
let index = temp.length
for(let h = index; h > 0; h--) {
switch(temp[h]) {
case '.': return;
case '/': this.setState (state =>({
screenValue1: `${state.screenValue1}${a}`
}));
return;
case 'x': this.setState (state =>({
screenValue1: `${state.screenValue1}${a}`
}));
return;
case '-': this.setState (state =>({
screenValue1: `${state.screenValue1}${a}`
}));
return;
case '+': this.setState (state =>({
screenValue1: `${state.screenValue1}${a}`
}));
return;
default: return;
}
}
return
}
if(a !== '.'){
this.setState (state =>({
screenValue1: `${state.screenValue1}${a}`
}))
}
}
}
render() {
return (
<div id="calc-root">
<div id="calc-background">
{/*<img src="https://i.postimg.cc/9fJ8nJtr/Calculator.png"/>*/}
<div id="calc-screen">
<p id="display">{this.state.screenValue1}</p>
</div>
<div id="calc-buttons">
<div onClick={() => this.screenUpdate('clear')} className="calc-btn lg-buttons grey-buttons" id="clear"><p>AC</p></div>
<div onClick={() => this.screenUpdate('/')} className="calc-btn sm-buttons grey-buttons margin-left" id="divide"><p>/</p></div>
<div onClick={() => this.screenUpdate('x')} className="calc-btn sm-buttons orange-buttons margin-left" id="multiply"><p>x</p></div>
<div onClick={() => this.screenUpdate(7)} className="calc-btn sm-buttons grey-buttons margin-top" id="seven"><p>7</p></div>
<div onClick={() => this.screenUpdate(8)} className="calc-btn sm-buttons grey-buttons margin-left margin-top" id="eight"><p>8</p></div>
<div onClick={() => this.screenUpdate(9)} className="calc-btn sm-buttons grey-buttons margin-left margin-top" id="nine"><p>9</p></div>
<div onClick={() => this.screenUpdate('-')} className="calc-btn sm-buttons orange-buttons margin-left margin-top" id="subtract"><p>-</p></div>
<div onClick={() => this.screenUpdate(4)} className="calc-btn sm-buttons grey-buttons margin-top" id="four"><p>4</p></div>
<div onClick={() => this.screenUpdate(5)} className="calc-btn sm-buttons grey-buttons margin-left margin-top" id="five"><p>5</p></div>
<div onClick={() => this.screenUpdate(6)} className="calc-btn sm-buttons grey-buttons margin-left margin-top" id="six"><p>6</p></div>
<div onClick={() => this.screenUpdate('+')} className="calc-btn sm-buttons orange-buttons margin-left margin-top" id="add"><p>+</p></div>
<div onClick={() => this.screenUpdate(1)} className="calc-btn sm-buttons grey-buttons margin-top" id="one"><p>1</p></div>
<div onClick={() => this.screenUpdate(2)} className="calc-btn sm-buttons grey-buttons margin-left margin-top" id="two"><p>2</p></div>
<div onClick={() => this.screenUpdate(3)} className="calc-btn sm-buttons grey-buttons margin-left margin-top" id="three"><p>3</p></div>
<div onClick={() => this.screenUpdate('=')} className="calc-btn lg-button-vert orange-buttons margin-left margin-top" id="equals"><p>=</p></div>
<div onClick={() => this.screenUpdate(0)} className="calc-btn lg-buttons grey-buttons" id="zero"><p>0</p></div>
<div onClick={() => this.screenUpdate('.')} className="calc-btn sm-buttons grey-buttons margin-left" id="decimal"><p>.</p></div>
</div>
</div>
</div>
)
}
}

export default Calculator;
104 changes: 104 additions & 0 deletions src/styles/pages/activities/calculator.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
#calc-root {
display: flex;
justify-content: center;
align-items: center;
padding-top: 100px;
}
#calc-background {
height: 450px;
width: 300px;
border-radius: 15px;
background-color: black;
display: flex;
flex-wrap: wrap;
}
#calc-screen {
position: relative;
margin: 0 auto;
margin-top: 15px;
height: 70px;
width: 270px;
border-radius: 15px;
background-color: #2b3025;
}
#display {
color: white;
font-family: arial;
position: absolute;
top: 35px;
right: 10px;
font-size: 30px;
z-index: 1;
}
#calc-buttons {
position: relative;
height: 340px;
width: 270px;
margin: 0 auto;
display: flex;
flex-wrap: wrap;
align-content: flex-start;
}
.lg-buttons {
height: 60px;
width: 130px;
border-radius: 50px;
display: flex;
}
.lg-button-vert {
height: 130px;
width: 60px;
border-radius: 50px;
display: flex;
}
.sm-buttons {
height: 60px;
width: 60px;
border-radius: 50%;
display: flex;
}
.grey-buttons {
background-color: #c6c6c6;
}
.orange-buttons {
background-color: #ed9700;
}
.margin-left {
margin-left: 10px;
}
.margin-top {
margin-top: 10px;
}
#zero {
position: absolute;
top: 280px;
}
#decimal {
position: absolute;
top: 280px;
left: 130px;
}
.calc-btn.lg-buttons p{
font-family: arial;
font-weight: bold;
font-size: 25px;
margin: auto;
}
.calc-btn.sm-buttons p{
font-family: arial;
font-weight: bold;
font-size: 25px;
margin: auto;
}
.calc-btn.lg-button-vert p{
font-family: arial;
font-weight: bold;
font-size: 25px;
margin: auto;
}
p {
user-select: none;
}
.calc-btn:hover {
border: 3px solid white;
}