diff --git a/src/App.js b/src/App.js index 993d922..524af5d 100644 --- a/src/App.js +++ b/src/App.js @@ -1,7 +1,7 @@ import React, { Component } from "react"; import FunctionalComponent from "./components/FunctionalComponent"; -import ClassComponent from "./components/ClassComponent"; +import Calculator from "./components/Calculator"; import "./css/App.css"; @@ -10,7 +10,7 @@ class App extends Component { return (
- +
); } diff --git a/src/components/Calculator.js b/src/components/Calculator.js new file mode 100644 index 0000000..afb76d6 --- /dev/null +++ b/src/components/Calculator.js @@ -0,0 +1,122 @@ +import React, { Component } from 'react' + +class Calculator extends Component { + + //the beginning value of the state and numbers is 0. + state= { + total: 0, + num1:0, + num2:0 + } + + + + setNum=(e, num)=>{ + console.log(e.target.value) + //the num is being given the target value, which is set by the user. + this.setState({ + [num]:e.target.value + }) + } + + + + //CODE TO ADD + getTotal=()=>{ + const grandTotal = parseInt(this.state.num1)+parseInt(this.state.num2) + this.setState({ + //num1:0 and num2:0 resets the values to 0 once button is pressed. In addition,its giving us the total. + num1:0, + num2:0, + total: grandTotal + }) + } + + //CODE TO SUBTRACT + minusTotal=()=>{ + const grandTotal = parseInt(this.state.minusNum1)-parseInt(this.state.minusNum2) + this.setState({ + //num1:0 and num2:0 resets the values to 0 once button is pressed. In addition,its giving us the total. + minusNum1:0, + minusNum2:0, + total2: grandTotal + }) + } + + + multiplyTotal=()=>{ + const grandTotal = parseInt(this.state.multiplyNum1)*parseInt(this.state.multiplyNum2) + this.setState({ + //num1:0 and num2:0 resets the values to 0 once button is pressed. In addition,its giving us the total. + multiplyNum1:0, + multiplyNum2:0, + total3: grandTotal + }) + } + + divideTotal=()=>{ + const grandTotal = parseInt(this.state.divideNum1)/parseInt(this.state.divideNum2) + this.setState({ + //num1:0 and num2:0 resets the values to 0 once button is pressed. In addition,its giving us the total. + divideNum1:0, + divideNum2:0, + total4: grandTotal + }) + } + + + render() { + return ( +
+ + {/* code below is to ADD */} +
+

Add with React!

+ this.setNum (e, 'num1')} /> + + + this.setNum(e, 'num2')} /> + +

Addition results go here!

+

{this.state.total}

+
+ + {/* code below is to subtract */} +
+

subtract with React!

+ this.setNum (e, 'minusNum1')} /> + - + this.setNum(e, 'minusNum2')} /> + +

subtraction results go here!

+

{this.state.total2}

+
+ + {/* code below is to multiply */} +
+

Multiply with React!

+ this.setNum (e, 'multiplyNum1')} /> + * + this.setNum(e, 'multiplyNum2')} /> + +

Multiplication results go here!

+

{this.state.total3}

+
+ + {/* code below is to multiply */} +
+

Divide with React!

+ this.setNum (e, 'divideNum1')} /> + / + this.setNum(e, 'divideNum2')} /> + +

Division results go here!

+

{this.state.total4}

+
+ + +
+ ) + } +} + +export default Calculator diff --git a/src/components/ClassComponent.js b/src/components/ClassComponent.js deleted file mode 100644 index fdf0e4f..0000000 --- a/src/components/ClassComponent.js +++ /dev/null @@ -1,13 +0,0 @@ -import React, { Component } from 'react' - -class ClassComponent extends Component { - render() { - return ( -
- Class Component -
- ) - } -} - -export default ClassComponent diff --git a/src/css/App.css b/src/css/App.css index 4931613..6d57e25 100644 --- a/src/css/App.css +++ b/src/css/App.css @@ -1,3 +1,23 @@ .App { text-align: center; } + +.container{ + display:flex-start; +} + +.add{ + background-color:rgb(200, 192, 192); +} + +.subtract{ + background-color:yellow; +} + +.multiply{ + background-color:green; +} + +.divide{ + background-color:pink; +} \ No newline at end of file