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
23 changes: 23 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
/node_modules
/.pnp
.pnp.js

# testing
/coverage

# production
/build

# misc
.DS_Store
.env.local
.env.development.local
.env.test.local
.env.production.local

npm-debug.log*
yarn-debug.log*
yarn-error.log*
14,005 changes: 14,005 additions & 0 deletions package-lock.json

Large diffs are not rendered by default.

34 changes: 34 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"name": "react_calculator",
"version": "0.1.0",
"private": true,
"dependencies": {
"@testing-library/jest-dom": "^4.2.4",
"@testing-library/react": "^9.4.0",
"@testing-library/user-event": "^7.2.1",
"react": "^16.12.0",
"react-dom": "^16.12.0",
"react-scripts": "3.3.0"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": "react-app"
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
}
}
Binary file added public/favicon.ico
Binary file not shown.
43 changes: 43 additions & 0 deletions public/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<link rel="icon" href="%PUBLIC_URL%/favicon.ico" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="theme-color" content="#000000" />
<meta
name="description"
content="Web site created using create-react-app"
/>
<link rel="apple-touch-icon" href="%PUBLIC_URL%/logo192.png" />
<!--
manifest.json provides metadata used when your web app is installed on a
user's mobile device or desktop. See https://developers.google.com/web/fundamentals/web-app-manifest/
-->
<link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
<!--
Notice the use of %PUBLIC_URL% in the tags 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>
<noscript>You need to enable JavaScript to run this app.</noscript>
<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` or `yarn start`.
To create a production bundle, use `npm run build` or `yarn build`.
-->
</body>
</html>
Binary file added public/logo192.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/logo512.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
25 changes: 25 additions & 0 deletions public/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"short_name": "React App",
"name": "Create React App Sample",
"icons": [
{
"src": "favicon.ico",
"sizes": "64x64 32x32 24x24 16x16",
"type": "image/x-icon"
},
{
"src": "logo192.png",
"type": "image/png",
"sizes": "192x192"
},
{
"src": "logo512.png",
"type": "image/png",
"sizes": "512x512"
}
],
"start_url": ".",
"display": "standalone",
"theme_color": "#000000",
"background_color": "#ffffff"
}
2 changes: 2 additions & 0 deletions public/robots.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# https://www.robotstxt.org/robotstxt.html
User-agent: *
39 changes: 39 additions & 0 deletions src/function/calculate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import clearAll from './clearAll'
import inputNumber from './inputNumber'
import decimalPoint from './decimalPoint'
import inputResult from './inputResult'
import inputContinuity from './inputContinuity'

function calculate(state, button) {
if (button === "C") {
return clearAll()
}

if ("0123456789".indexOf(button) !== -1) {
return inputNumber(state,button)
}

if (button === ".") {
return decimalPoint(state.nextValue)
}

if (button === "=") {
return inputResult(state)
}

if (state.operatorValue) {
return inputContinuity(state,button)
}

if (!state.nextValue) {
return { operatorValue: button };
}

return {
displayValue: state.nextValue,
nextValue: null,
operatorValue: button,
};
}

export default calculate;
9 changes: 9 additions & 0 deletions src/function/clearAll.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
function clearAll(){
return {
displayValue: "0",
nextValue: null,
operatorValue: null,
};
}

export default clearAll
11 changes: 11 additions & 0 deletions src/function/decimalPoint.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
function decimalPoint(next){
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

함수명은 Naming Convention에 의거하여 동사를 사용해야 합니다.

if (next) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if(next)가 의미하는게 명확하지 않습니다.
next === NaN, next === undefined 이런식으로 정확히 명시해주세요.

if (next.includes(".")) {
return {};
}
return { nextValue: next + "." };
}
return { nextValue: "0." };
Comment on lines +2 to +8

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

들여쓰기 간격 맞춰주세요!

}

export default decimalPoint
25 changes: 25 additions & 0 deletions src/function/doOperation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
function doOperation(state) {
const operator = state.operatorValue
const preOperand = parseFloat(state.displayValue || "0")
const backOperand = parseFloat(state.nextValue || (operator === "/" || operator === '*' ? "1": "0"))
Comment on lines +3 to +4

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

preOperand, backOperand라는 변수명보다 lhs, rhs 또는 lhsOperand, rhsOperand를 사용하는 것을 추천합니다. left-hand side, right-hand side의 약자입니다.
state.displayValue || "0"null 처리하는 것은 좋아요! 👍

if (operator === "+") {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

여유가 된다면 string연산자와 대응하는 함수를 객체로 묶어서 개발해 보는걸 추천합니다.

return (preOperand+backOperand).toString();
}
if (operator === "-") {
return (preOperand-backOperand).toString();
}
if (operator === "*") {
return (preOperand*backOperand).toString();
}
if (operator === "/") {
if (backOperand == "0") {
return "NaN";
}
if (backOperand != "0"){
return (preOperand/backOperand).toString();
}
}
console.log('error');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이번 요구사항에 console 사용은 없습니다! 지워주세요.

}

export default doOperation;
10 changes: 10 additions & 0 deletions src/function/inputContinuity.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import doOperation from './doOperation'

function inputContinuity(state,button){
return {
displayValue: doOperation(state),
nextValue: null,
operatorValue: button,
};
}
export default inputContinuity
30 changes: 30 additions & 0 deletions src/function/inputNumber.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
function inputNumber(state,button){
const next = state.nextValue
const operator = state.operatorValue

if (button === "0" && next === "0") {
return {};
}

if (operator) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indent가 맞지 않네요

if (next) {
return { nextValue: next + button };
}
return { nextValue: button };
}

if (next) {
const nextValue = next === "0" ? button : next + button;
return {
nextValue: nextValue,
displayValue: null,
};
}
return {
nextValue: button,
displayValue: null,
};
}


export default inputNumber
13 changes: 13 additions & 0 deletions src/function/inputResult.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import doOperation from './doOperation'

function inputResult(state){
if (state.nextValue && state.operatorValue) {
return {
displayValue: doOperation(state),
nextValue: null,
operatorValue: null,
};
}
}

export default inputResult
5 changes: 5 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import React from 'react';
import ReactDOM from 'react-dom';
import Calculator from './layout/reactCalculator';

ReactDOM.render(<Calculator />, document.getElementById('root'));
37 changes: 37 additions & 0 deletions src/layout/buttonLayout.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import React, {Component, Fragment } from 'react';

class buttonLayout extends Component{
render(){
return(
<Fragment>
<button name="C" onClick={event => this.props.click(event.target.name)}>C</button>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

버튼도 모두 React Component로 만들어보는 걸 추천합니다.

<div>
<button name="7" onClick={event => this.props.click(event.target.name)}>7</button>
<button name="8" onClick={event => this.props.click(event.target.name)}>8</button>
<button name="9" onClick={event => this.props.click(event.target.name)}>9</button>
<button name="/" onClick={event => this.props.click(event.target.name)}>/</button>
</div>
<div>
<button name="4" onClick={event => this.props.click(event.target.name)}>4</button>
<button name="5" onClick={event => this.props.click(event.target.name)}>5</button>
<button name="6" onClick={event => this.props.click(event.target.name)}>6</button>
<button name="*" onClick={event => this.props.click(event.target.name)}>*</button>
</div>
<div>
<button name="1" onClick={event => this.props.click(event.target.name)}>1</button>
<button name="2" onClick={event => this.props.click(event.target.name)}>2</button>
<button name="3" onClick={event => this.props.click(event.target.name)}>3</button>
<button name="-" onClick={event => this.props.click(event.target.name)}>-</button>
</div>
<div>
<button name="0" onClick={event => this.props.click(event.target.name)}>0</button>
<button name="." onClick={event => this.props.click(event.target.name)}>.</button>
<button name="=" onClick={event => this.props.click(event.target.name)}>=</button>
<button name="+" onClick={event => this.props.click(event.target.name)}>+</button>
</div>
</Fragment>
)
}
}

export default buttonLayout;
12 changes: 12 additions & 0 deletions src/layout/displayLayout.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import React, {Component } from 'react';

class displayLayout extends Component{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

클래스명은 대문자로 시작합니다.

render(){
const displayStyle = {textAlign:"right"}
return(
<input type="text" readOnly style={displayStyle} value={this.props.value}></input>
)
}
}

export default displayLayout;
56 changes: 56 additions & 0 deletions src/layout/reactCalculator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import React, {Component } from 'react';
import calculate from "../function/calculate"
import DisplayLayout from "./displayLayout"
import ButtonLayout from "./buttonLayout"

class reactCalculator extends Component{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

클래스명은 대문자로 시작합니다

constructor(){
super()
this.state = {
nextValue: null,
operatorValue: null,
displayValue: null
}
}

buttonClick = (buttonName) => {
this.setState(calculate(this.state, buttonName))
}

inputKeyboard = (event) => {
let {key} = event
const operations = ['/','*','+','-','.','=']

if (key === 'Enter')
key = '='

if ((/\d/).test(key)) {
event.preventDefault()
this.buttonClick(key)
}
if (operations.indexOf(key)!= -1) {
event.preventDefault()
this.buttonClick(key)
}
};

componentDidMount() {
document.addEventListener('keydown', this.inputKeyboard)
}

componentWillUnmount() {
document.removeEventListener('keydown', this.inputKeyboard)
}

render(){
return(
<div>
<DisplayLayout value = {this.state.nextValue || this.state.displayValue}></DisplayLayout>
<ButtonLayout click = {this.buttonClick}></ButtonLayout>
</div>
)
}
}

export default reactCalculator;