Last active
October 10, 2016 17:07
-
-
Save iZaL/42497c50ef89dfa09dcc74b84a04612e to your computer and use it in GitHub Desktop.
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
import React,{ Component } from 'react'; | |
import { View,Text,StyleSheet} from 'react-native'; | |
export default class App extends Component { | |
constructor() { | |
super(); | |
this.state = { | |
loanAmount:'50000', | |
interestRate:'5', | |
loanPeriod:'5', | |
loanTenure:'4', | |
totalAmount:'', | |
}; | |
this.calculate = this.calculate.bind(this); | |
} | |
onChange(name,value) { | |
switch (name) { | |
case 'loanAmount': | |
this.setState({ | |
loanAmount:value | |
}); | |
break; | |
case 'interestRate': | |
this.setState({ | |
interestRate:value | |
}); | |
case 'loanPeriod': | |
this.setState({ | |
loanPeriod:value | |
}); | |
case 'loanTenure': | |
this.setState({ | |
loanTenure:value | |
}); | |
break; | |
default : | |
return; | |
} | |
} | |
calculate() { | |
const {loanAmount,interestRate,loanPeriod,loanTenure} = this.state; | |
const i = (parseInt(interestRate)/100).toFixed(2); // convert 5 to 0.05 | |
const J = i/loanTenure; | |
const B = 1 - (1 / Math.pow(1+J,loanPeriod * loanTenure)); | |
const finalAmount = loanAmount * (J/B); | |
const amountPayable = Math.round(finalAmount * loanPeriod * loanTenure); | |
const interestAmount = amountPayable - loanAmount; | |
console.log('amount Payable',amountPayable); | |
console.log('interest amount',interestAmount); | |
} | |
render() { | |
const {loanAmount,interestRate,loanPeriod,loanTenure,totalAmount} = this.state; | |
return ( | |
<View style={styles.container}> | |
<Text onPress={()=>this.calculate()} style={styles.resultText}>Calculate </Text> | |
</View> | |
) | |
} | |
} | |
const styles = StyleSheet.create({ | |
container:{ | |
flex:1, | |
paddingTop:64 | |
}, | |
resultText: { | |
fontWeight:'700', | |
fontSize:19, | |
marginTop:10, | |
color:'black', | |
textAlign:'center' | |
}, | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment