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
17 changes: 17 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "hodaessi_calculator",
"version": "0.1.0",
"private": true,
"dependencies": {
"react": "^16.12.0",
"react-dom": "^16.12.0",
"react-scripts": "0.9.5"
},
"devDependencies": {},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test --env=jsdom",
"eject": "react-scripts eject"
}
}
Binary file added public/favicon.ico
Binary file not shown.
31 changes: 31 additions & 0 deletions public/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico">
<!--
Notice the use of %PUBLIC_URL% in the tag 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>
<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`.
To create a production bundle, use `npm run build`.
-->
</body>
</html>
16 changes: 16 additions & 0 deletions src/App.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
.App {
display: flex;
justify-content: center;
align-items: enter;
height: 100vh;
width: 100%;
margin-top: 50px;
}
.calculation {
width: 400px;
height: 600px;
}
.row {
display: flex;
widthL: 100%;
}
137 changes: 137 additions & 0 deletions src/App.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
//https://www.youtube.com/watch?v=nImdvUVMfl0
import React, { Component } from 'react';
import './App.css';
import Button from './components/Button';
import Input from './components/Input';
import ClearButton from './components/ClearButton';

class App extends Component {
constructor(props) {
super(props);

this.state = {
input: "",
previousNumber: "",
currentNumber: "",
operator: ""
};
}

addToInput = val => {
this.setState({input: this.state.input + val});
};

addZeroToInput = val => {
if (this.state.input !== "") {
this.setState({input: this.state.input + val});
}
}

addDecimalToInput = val => {
if (this.state.input.indexOf(".") === -1) {
this.setState({input: this.state.input + val});
}
}

clearInput = () => {
this.setState({ input: "" });
}

add = () => {
this.state.previousNumber = this.state.input;
this.setState({input: ""});
this.state.operator = "plus";
}
subtract = () => {
this.state.previousNumber = this.state.input;
this.setState({input: ""});
this.state.operator = "subtract";
}
multiply = () => {
this.state.previousNumber = this.state.input;
this.setState({input: ""});
this.state.operator = "multiply";
}
divide = () => {
this.state.previousNumber = this.state.input;
this.setState({input: ""});
this.state.operator = "divide";
}

evaluation = () => {
this.state.currentNumber = this.state.input;

if(this.state.operator == "plus") {
this.setState({
input:
parseFloat(this.state.previousNumber) +
parseFloat(this.state.currentNumber)
})
} else if(this.state.operator == "subtract") {
this.setState({
input:
parseFloat(this.state.previousNumber) -
parseFloat(this.state.currentNumber)
})
} else if(this.state.operator == "multiply") {
this.setState({
input:
parseFloat(this.state.previousNumber) *
parseFloat(this.state.currentNumber)
})
} else if(this.state.operator == "divide") {
this.setState({
input:
parseFloat(this.state.previousNumber) /
parseFloat(this.state.currentNumber)
})
}
}

render() {
return (
<div className="App">
<div className="calculation">
<div className="row">
<Input>{this.state.input}</Input>
</div>
<div className="row">
<Button handleClick={this.addToInput}>7</Button>
<Button handleClick={this.addToInput}>8</Button>
<Button handleClick={this.addToInput}>9</Button>

<Button handleClick={this.divide}>/</Button>
</div>
<div className="row">
<Button handleClick={this.addToInput}>4</Button>
<Button handleClick={this.addToInput}>5</Button>
<Button handleClick={this.addToInput}>6</Button>

<Button handleClick={this.multiply}>*</Button>
</div>
<div className="row">
<Button handleClick={this.addToInput}>1</Button>
<Button handleClick={this.addToInput}>2</Button>
<Button handleClick={this.addToInput}>3</Button>

<Button handleClick={this.add}>+</Button>
</div>
<div className="row">
<Button handleClick={this.addDecimalToInput}>.</Button>

<Button handleClick={this.addZeroToInput}>0</Button>

<Button handleClick={this.evaluation}>=</Button>

<Button handleClick={this.subtract}>-</Button>
</div>
<div className="row">
<ClearButton handleClear={this.clearInput}>Clear</ClearButton>
</div>
</div>
</div>
);
}
}

export default App;
8 changes: 8 additions & 0 deletions src/App.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';

it('renders without crashing', () => {
const div = document.createElement('div');
ReactDOM.render(<App />, div);
});
16 changes: 16 additions & 0 deletions src/components/Button.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
.button {
display: flex;
height: 4em;
flex: 1;
justify-content: center;
align-items: center;
font-weight: lighter;
font-size: 1.4em;
background-color: #e0e1e6;
color: black;
outline: 1px solid gray;
}
.operator {
background-color: #fe9241;
color: white;
}
23 changes: 23 additions & 0 deletions src/components/Button.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import React, { Component } from 'react';
import './Button.css';

class Button extends Component {
isOperator = val => {
return !isNaN(val) || val === "." || val === "=";
};

render() {
return (
<div
className={`button ${this.isOperator(
this.props.children) ? "" : "operator"}`}
onClick={() => this.props.handleClick(
this.props.children)}
>
{this.props.children}
</div>
);
}
}

export default Button;
12 changes: 12 additions & 0 deletions src/components/ClearButton.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
.clear-button{
display: flex;
flex: 1;
height: 4em;
align-items: center;
justify-content: center;
font-weight: lighter;
font-size: 1.rem;
outline: 1px solid #888;
height: 4em;
background-color: #c5c3cd;
}
17 changes: 17 additions & 0 deletions src/components/ClearButton.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import React, { Component } from 'react';
import './ClearButton.css';

class ClearButton extends Component {
render() {
return (
<div
className="clear-button"
onClick={() => this.props.handleClear()}
>
{this.props.children}
</div>
);
}
}

export default ClearButton;
13 changes: 13 additions & 0 deletions src/components/Input.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
.input {
display: flex;
height: 4em;
flex: 1;
background-color: #000000;
outline: 1px solid #888888;
color: #fff;
justify-content: flex-end;
padding: 0.5em;
font-weight: bold;
font-size: 1.4em;
align-items: center;
}
14 changes: 14 additions & 0 deletions src/components/Input.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import React, { Component } from 'react';
import './Input.css';

class Input extends Component {
render() {
return (
<div className="input">
{this.props.children}
</div>
);
}
}

export default Input;
5 changes: 5 additions & 0 deletions src/index.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
body {
margin: 0;
padding: 0;
font-family: sans-serif;
}
9 changes: 9 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';
import './index.css';

ReactDOM.render(
<App />,
document.getElementById('root')
);
7 changes: 7 additions & 0 deletions src/logo.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.