-
-
Notifications
You must be signed in to change notification settings - Fork 85
Clean code #23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
karanWD
wants to merge
10
commits into
niinpatel:master
Choose a base branch
from
karanWD:clean-code
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Clean code #23
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
9f64112
convert class components to fuctional components
33f7766
modify ui of elements
d25e1ed
modify ui
bca5a43
Update README.md
karanWD 7c3a64b
replace eval with Function
10363e4
Merge branch 'master' of https://github.com/karanWD/bootCamp-calc
411915a
implement arrays
83e82df
modify if elses with switch
ce6219f
modify if elses with switch
d32e5d1
add texts to object
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,27 +1,59 @@ | ||
| .App{ | ||
| height: 100vh; | ||
| width: 100%; | ||
| display: flex; | ||
| align-items: center; | ||
| justify-content: center; | ||
| background-color:#444; | ||
| } | ||
| .result { | ||
| height: 60px; | ||
| background-color: #bbb; | ||
| height: 150px; | ||
| background-color: #111; | ||
| width: 100%; | ||
| border-radius: 10px 10px 0 0 ; | ||
| } | ||
|
|
||
|
|
||
| .result p { | ||
| font-size: 40px; | ||
| margin: 5px; | ||
|
|
||
| color: white; | ||
| padding: 15px; | ||
| } | ||
|
|
||
| .calculator-body { | ||
| max-width: 400px; | ||
| width: 400px; | ||
| margin: auto; | ||
| } | ||
|
|
||
| .button { | ||
| display: block; | ||
| background-color: #bbb; | ||
| } | ||
|
|
||
| button { | ||
| background-color: #555; | ||
| width: 25%; | ||
| height: 60px; | ||
| height: 80px; | ||
| font-size: 30px; | ||
| cursor:pointer; | ||
| border:1px solid #333; | ||
| color: white; | ||
| } | ||
| button:hover{ | ||
| background-color: #666; | ||
| } | ||
| .actions{ | ||
| background-color: #444; | ||
| color: #ef6431; | ||
| } | ||
| .actions:hover{ | ||
| background-color: #333; | ||
| } | ||
| .equal{ | ||
| background-color: orangered; | ||
| color: white; | ||
| } | ||
| .equal:hover{ | ||
| background-color: rgb(255, 181, 154); | ||
| color: orangered; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,84 +1,55 @@ | ||
| import React, { Component } from 'react'; | ||
| import React, {useState} from 'react'; | ||
| import './App.css'; | ||
| import ResultComponent from './components/ResultComponent'; | ||
| import KeyPadComponent from "./components/KeyPadComponent"; | ||
|
|
||
| class App extends Component { | ||
| constructor(){ | ||
| super(); | ||
|
|
||
| this.state = { | ||
| result: "" | ||
| } | ||
| } | ||
|
|
||
| onClick = button => { | ||
|
|
||
| if(button === "="){ | ||
| this.calculate() | ||
| } | ||
|
|
||
| else if(button === "C"){ | ||
| this.reset() | ||
| } | ||
| else if(button === "CE"){ | ||
| this.backspace() | ||
| } | ||
|
|
||
| else { | ||
| this.setState({ | ||
| result: this.state.result + button | ||
| }) | ||
| } | ||
| }; | ||
|
|
||
|
|
||
| calculate = () => { | ||
| const App =()=> { | ||
| const [result, setResult] = useState("") | ||
| const calculate = () => { | ||
| var checkResult = '' | ||
| if(this.state.result.includes('--')){ | ||
| checkResult = this.state.result.replace('--','+') | ||
| if (result.includes('--')) { | ||
| checkResult = result.replace('--', '+') | ||
| } else { | ||
| checkResult = result | ||
| } | ||
|
|
||
| else { | ||
| checkResult = this.state.result | ||
| } | ||
|
|
||
| try { | ||
| this.setState({ | ||
| // eslint-disable-next-line | ||
| result: (eval(checkResult) || "" ) + "" | ||
| }) | ||
| // let stringRes = (eval(checkResult) || "").toString() | ||
| let stringRes = Function('"use strict";return ('+ (checkResult||"") +')')().toString() | ||
| setResult(stringRes) | ||
| } catch (e) { | ||
| this.setState({ | ||
| result: "error" | ||
| }) | ||
|
|
||
| setResult("error") | ||
| } | ||
| }; | ||
| } | ||
| const reset = () => { | ||
| setResult("") | ||
| } | ||
| const backspace = () => { | ||
| setResult(prevState => prevState.slice(0, -1)) | ||
| } | ||
|
|
||
| reset = () => { | ||
| this.setState({ | ||
| result: "" | ||
| }) | ||
| }; | ||
| const onClick = (button) => { | ||
| switch (button){ | ||
| case "=" : return calculate() | ||
| case "C" : return reset() | ||
| case "CE" : return backspace() | ||
| default : setResult(prevState => prevState+button) | ||
| } | ||
| } | ||
|
|
||
| backspace = () => { | ||
| this.setState({ | ||
| result: this.state.result.slice(0, -1) | ||
| }) | ||
| }; | ||
| return ( | ||
| <div className={`App`}> | ||
| <div className="calculator-body"> | ||
| <ResultComponent result={result}/> | ||
| <KeyPadComponent onClick={onClick}/> | ||
| </div> | ||
|
|
||
| render() { | ||
| return ( | ||
| <div> | ||
| <div className="calculator-body"> | ||
| <h1>Simple Calculator</h1> | ||
| <ResultComponent result={this.state.result}/> | ||
| <KeyPadComponent onClick={this.onClick}/> | ||
| </div> | ||
| <div style={{backgroundColor:"white"}}> | ||
| </div> | ||
| ); | ||
| } | ||
|
|
||
| </div> | ||
| ) | ||
| } | ||
|
|
||
| export default App; | ||
| export default App | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,40 +1,56 @@ | ||
| import React, {Component} from 'react'; | ||
|
|
||
| class KeyPadComponent extends Component { | ||
|
|
||
| render() { | ||
| return ( | ||
| <div className="button"> | ||
| <button name="(" onClick={e => this.props.onClick(e.target.name)}>(</button> | ||
| <button name="CE" onClick={e => this.props.onClick(e.target.name)}>CE</button> | ||
| <button name=")" onClick={e => this.props.onClick(e.target.name)}>)</button> | ||
| <button name="C" onClick={e => this.props.onClick(e.target.name)}>C</button><br/> | ||
|
|
||
|
|
||
| <button name="1" onClick={e => this.props.onClick(e.target.name)}>1</button> | ||
| <button name="2" onClick={e => this.props.onClick(e.target.name)}>2</button> | ||
| <button name="3" onClick={e => this.props.onClick(e.target.name)}>3</button> | ||
| <button name="+" onClick={e => this.props.onClick(e.target.name)}>+</button><br/> | ||
|
|
||
|
|
||
| <button name="4" onClick={e => this.props.onClick(e.target.name)}>4</button> | ||
| <button name="5" onClick={e => this.props.onClick(e.target.name)}>5</button> | ||
| <button name="6" onClick={e => this.props.onClick(e.target.name)}>6</button> | ||
| <button name="-" onClick={e => this.props.onClick(e.target.name)}>-</button><br/> | ||
|
|
||
| <button name="7" onClick={e => this.props.onClick(e.target.name)}>7</button> | ||
| <button name="8" onClick={e => this.props.onClick(e.target.name)}>8</button> | ||
| <button name="9" onClick={e => this.props.onClick(e.target.name)}>9</button> | ||
| <button name="*" onClick={e => this.props.onClick(e.target.name)}>x</button><br/> | ||
|
|
||
|
|
||
| <button name="." onClick={e => this.props.onClick(e.target.name)}>.</button> | ||
| <button name="0" onClick={e => this.props.onClick(e.target.name)}>0</button> | ||
| <button name="=" onClick={e => this.props.onClick(e.target.name)}>=</button> | ||
| <button name="/" onClick={e => this.props.onClick(e.target.name)}>÷</button><br/> | ||
| </div> | ||
| ); | ||
| } | ||
| import React from 'react'; | ||
|
|
||
|
|
||
| const buttons = [ | ||
| [ | ||
| {text:"(",value:"(",className:"actions"}, | ||
| {text:"CE",value:"CE",className:"actions"}, | ||
| {text:")",value:")",className:"actions"}, | ||
| {text:"C",value:"C",className:"actions"}, | ||
| ], | ||
| [ | ||
| {text:"1",value:"1",className:""}, | ||
| {text:"2",value:"2",className:""}, | ||
| {text:"3",value:"3",className:""}, | ||
| {text:"+",value:"+",className:"actions"}, | ||
| ], | ||
| [ | ||
| {text:"4",value:"4",className:""}, | ||
| {text:"5",value:"5",className:""}, | ||
| {text:"6",value:"6",className:""}, | ||
| {text:"-",value:"-",className:"actions"}, | ||
| ], | ||
| [ | ||
| {text:"7",value:"7",className:""}, | ||
| {text:"8",value:"8",className:""}, | ||
| {text:"9",value:"9",className:""}, | ||
| {text:"x",value:"*",className:"actions"}, | ||
| ], | ||
| [ | ||
| {text:".",value:".",className:""}, | ||
| {text:"0",value:"0",className:""}, | ||
| {text:"=",value:"=",className:"equal"}, | ||
| {text:"÷",value:"/",className:"actions"}, | ||
| ], | ||
| ] | ||
| const KeyPadComponent = ({onClick}) => { | ||
|
|
||
| return ( | ||
| <div className="button"> | ||
| { | ||
| buttons.map((rows) => ( | ||
| <> | ||
| {rows.map((item, index) => { | ||
| return ( | ||
| <button key={index} className={item.className} onClick={() => onClick(item.value)}>{item.text}</button>) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not a good practice to keep index as key. |
||
| })} | ||
| <br/> | ||
| </> | ||
|
|
||
| )) | ||
| } | ||
| </div> | ||
| ); | ||
| } | ||
|
|
||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,19 +1,12 @@ | ||
| import React, {Component} from 'react'; | ||
| import React from 'react'; | ||
|
|
||
| class ResultComponent extends Component { | ||
|
|
||
|
|
||
| render() { | ||
| let {result} = this.props; | ||
| return ( | ||
| <div className="result"> | ||
| <p>{result}</p> | ||
| </div> | ||
| const ResultComponent = ({result}) => { | ||
| return ( | ||
| <div className="result"> | ||
| <p>{result}</p> | ||
| </div> | ||
| ) | ||
| ; | ||
| } | ||
| } | ||
|
|
||
|
|
||
| export default ResultComponent; | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Commented code should not be pushed to GitHub.