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
115 changes: 111 additions & 4 deletions challenges/calculadora/src/components/Calculator.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,126 @@ export default class Calculator extends Component {
this.clearMemory = this.clearMemory.bind(this)
this.setOperation = this.setOperation.bind(this)
this.addDigit = this.addDigit.bind(this)
this.calculate = this.calculate.bind(this)
}

//Seu objetivo é implementar estas funções

calculate(){
/*
this method will calculate the result and show it on the display
*/
let result = 0
switch(this.state.operation){
case '+':
result = this.state.values[0] + this.state.values[1]
break;
case '-':
result = this.state.values[0] - this.state.values[1]
break;
case '*':
result = this.state.values[0] * this.state.values[1]
break;
case '/':
result = this.state.values[0] / this.state.values[1]
break;
default: break;
}
const resultString = result.toString()

/*
it verifies if the number have more than 9 digits. If true, it verifies if the number has 10 digits and is
a negative number. If true, the method simply displays the number with the minus sign before it
*/
if(resultString.length > 9){
if(resultString.length === 10 && result < 0){
this.setState({
...this.state,
displayValue: resultString,
clearDisplay: true,
})
}
else{
this.setState({
...this.state,
displayValue: 'Erro',
})
}
}
else{
this.setState({
...this.state,
displayValue: resultString,
clearDisplay: true,
})
}
}


clearMemory() {
// ERrrRRooOrRR: 8932183!
/*
this method will reset the state to the initial state, clearing the memory
*/
this.setState({
...initialState,
})
}

setOperation(operation) {
// ERrrRRooOrRR: 2162621 ;-;!
async setOperation(operation) {
if(operation === '='){
/*
if the operation is '=', the method will await the setState and then call the calculate method
*/
await this.setState({
...this.state,
values: [this.state.values[0], parseFloat(this.state.displayValue)]
})
this.calculate()
}
else{
/*
if the operation is not '=', the method will set the operation in state to the operation called
*/
this.setState({
...this.state,
operation: operation,
clearDisplay: true,
})
}
}

addDigit(n) {
// ErrrroR: 7163271 ;-;
/*
it will verify if the display value has less than 9 digits. If the number has more than 9 digits,
it does nothing for aesthetic purposes, cause more than 9 digits on the display causes the exceeding
digits to disappear from the display, making difficult to see the real number that is on the state
*/
if(this.state.displayValue.length === 9 && !this.state.clearDisplay);

// it will verify if it is the first number of the operation or not
else if(!this.state.clearDisplay){
/*
if it is the first number of the operation, the method will verify if there is only a 0 in the display;
if true, it will replace the zero with the digit clicked;
if false, it will put the number in front of the last added digit;
*/
this.setState({
...this.state,
displayValue: this.state.displayValue === '0' ? n.toString() : this.state.displayValue + n.toString(),
})
}
else{
/*
if there was a number already added to the operation, the method will replace the entire number in the display
for the digit clicked
*/
this.setState({
...this.state,
clearDisplay: false,
values: [parseFloat(this.state.displayValue), 0],
displayValue: n,
})
}
}

render() {
Expand Down
2 changes: 1 addition & 1 deletion challenges/calculadora/src/components/Header.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export default function() {
return <header>
<h1>Commit Pull Hackquest 2020</h1>
<h2>
<img class="commit" src="https://www.commitjr.com/wp-content/uploads/2020/06/commit-jr_arara_512_512.png"/>
<img class="commit" alt="Commit Jr logo" src="https://www.commitjr.com/wp-content/uploads/2020/06/commit-jr_arara_512_512.png"/>
</h2>
</header>;
}
2 changes: 1 addition & 1 deletion challenges/calculadora/src/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { Fragment } from 'react';
import React from 'react';
import ReactDom from 'react-dom';

import './index.css';
Expand Down