React function mortgage calculator returns NAN

Multi tool use
up vote
0
down vote
favorite
The function handleCostChange is supposed to calculate the mortgage payment based on the inputs. It currently returns NAN- I first wrote it in JavaScript and it worked well. I'm trying to figure out what went wrong when I tried to refactor this function from Javascript and add it to my React component.
import React, { Component } from 'react';
import Counter from './components/Counter';
import './App.css';
class App extends Component {
state = {
cost: 0,
houseCost: 0,
downPayment: 0,
termOfLoan: 0,
annualInterestRate: 0
};
handleChange = (e) => {
this.setState({
houseCost: e.target.houseCost,
downPayment: e.target.downPayment,
termOfLoan: e.target.termOfLoan,
annualInterestRate: e.target.annualInterestRate
});
};
handleCostChange = () => {
const principal = this.state.houseCost - this.state.downPayment
const percentageRate = this.state.annualInterestRate / 1200
const lengthOfLoan = 12 * this.state.termOfLoan
this.setState(
prevState => ({
cost: (prevState.cost = (principal * percentageRate) / (1 - (Math.pow((1 + percentageRate) , lengthOfLoan * -1)))).toString()
});
);
};
render() {
return (
<div className="App">
<Counter
cost={this.state.cost}
houseCost={this.state.houseCost}
downPayment={this.state.downPayment}
termOfLoan={this.state.termOfLoan}
annualInterestRate={this.state.annualInterestRate}
changeCost={this.handleCostChange}
handleChange={this.handleChange}
/>
</div>
);
}
}
export default App;
Counter.js file
import React from 'react';
const Counter = (props) => {
return (
<div className="counter">
<input
type="number"
placeholder="House Cost"
onChange={(e) => props.handleChange(e)}>
</input>
<input
type="number"
placeholder="Down Payment"
onChange={(e) => props.handleChange(e)}>
</input>
<input
type="number"
placeholder="Mortgage Period (years)"
onChange={(e) => props.handleChange(e)}>
</input>
<input
type="number"
placeholder="Interest Rate"
onChange={(e) => props.handleChange(e)}>
</input>
<button
className="counter-action"
onClick={props.changeCost}
>Calculate
</button>
<span className="counter-score">{ props.cost }</span>
</div>
);
}
export default Counter;
javascript reactjs
add a comment |
up vote
0
down vote
favorite
The function handleCostChange is supposed to calculate the mortgage payment based on the inputs. It currently returns NAN- I first wrote it in JavaScript and it worked well. I'm trying to figure out what went wrong when I tried to refactor this function from Javascript and add it to my React component.
import React, { Component } from 'react';
import Counter from './components/Counter';
import './App.css';
class App extends Component {
state = {
cost: 0,
houseCost: 0,
downPayment: 0,
termOfLoan: 0,
annualInterestRate: 0
};
handleChange = (e) => {
this.setState({
houseCost: e.target.houseCost,
downPayment: e.target.downPayment,
termOfLoan: e.target.termOfLoan,
annualInterestRate: e.target.annualInterestRate
});
};
handleCostChange = () => {
const principal = this.state.houseCost - this.state.downPayment
const percentageRate = this.state.annualInterestRate / 1200
const lengthOfLoan = 12 * this.state.termOfLoan
this.setState(
prevState => ({
cost: (prevState.cost = (principal * percentageRate) / (1 - (Math.pow((1 + percentageRate) , lengthOfLoan * -1)))).toString()
});
);
};
render() {
return (
<div className="App">
<Counter
cost={this.state.cost}
houseCost={this.state.houseCost}
downPayment={this.state.downPayment}
termOfLoan={this.state.termOfLoan}
annualInterestRate={this.state.annualInterestRate}
changeCost={this.handleCostChange}
handleChange={this.handleChange}
/>
</div>
);
}
}
export default App;
Counter.js file
import React from 'react';
const Counter = (props) => {
return (
<div className="counter">
<input
type="number"
placeholder="House Cost"
onChange={(e) => props.handleChange(e)}>
</input>
<input
type="number"
placeholder="Down Payment"
onChange={(e) => props.handleChange(e)}>
</input>
<input
type="number"
placeholder="Mortgage Period (years)"
onChange={(e) => props.handleChange(e)}>
</input>
<input
type="number"
placeholder="Interest Rate"
onChange={(e) => props.handleChange(e)}>
</input>
<button
className="counter-action"
onClick={props.changeCost}
>Calculate
</button>
<span className="counter-score">{ props.cost }</span>
</div>
);
}
export default Counter;
javascript reactjs
Theinput
tags should be self-closing tags.
– hypnagogia
Nov 5 at 20:54
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
The function handleCostChange is supposed to calculate the mortgage payment based on the inputs. It currently returns NAN- I first wrote it in JavaScript and it worked well. I'm trying to figure out what went wrong when I tried to refactor this function from Javascript and add it to my React component.
import React, { Component } from 'react';
import Counter from './components/Counter';
import './App.css';
class App extends Component {
state = {
cost: 0,
houseCost: 0,
downPayment: 0,
termOfLoan: 0,
annualInterestRate: 0
};
handleChange = (e) => {
this.setState({
houseCost: e.target.houseCost,
downPayment: e.target.downPayment,
termOfLoan: e.target.termOfLoan,
annualInterestRate: e.target.annualInterestRate
});
};
handleCostChange = () => {
const principal = this.state.houseCost - this.state.downPayment
const percentageRate = this.state.annualInterestRate / 1200
const lengthOfLoan = 12 * this.state.termOfLoan
this.setState(
prevState => ({
cost: (prevState.cost = (principal * percentageRate) / (1 - (Math.pow((1 + percentageRate) , lengthOfLoan * -1)))).toString()
});
);
};
render() {
return (
<div className="App">
<Counter
cost={this.state.cost}
houseCost={this.state.houseCost}
downPayment={this.state.downPayment}
termOfLoan={this.state.termOfLoan}
annualInterestRate={this.state.annualInterestRate}
changeCost={this.handleCostChange}
handleChange={this.handleChange}
/>
</div>
);
}
}
export default App;
Counter.js file
import React from 'react';
const Counter = (props) => {
return (
<div className="counter">
<input
type="number"
placeholder="House Cost"
onChange={(e) => props.handleChange(e)}>
</input>
<input
type="number"
placeholder="Down Payment"
onChange={(e) => props.handleChange(e)}>
</input>
<input
type="number"
placeholder="Mortgage Period (years)"
onChange={(e) => props.handleChange(e)}>
</input>
<input
type="number"
placeholder="Interest Rate"
onChange={(e) => props.handleChange(e)}>
</input>
<button
className="counter-action"
onClick={props.changeCost}
>Calculate
</button>
<span className="counter-score">{ props.cost }</span>
</div>
);
}
export default Counter;
javascript reactjs
The function handleCostChange is supposed to calculate the mortgage payment based on the inputs. It currently returns NAN- I first wrote it in JavaScript and it worked well. I'm trying to figure out what went wrong when I tried to refactor this function from Javascript and add it to my React component.
import React, { Component } from 'react';
import Counter from './components/Counter';
import './App.css';
class App extends Component {
state = {
cost: 0,
houseCost: 0,
downPayment: 0,
termOfLoan: 0,
annualInterestRate: 0
};
handleChange = (e) => {
this.setState({
houseCost: e.target.houseCost,
downPayment: e.target.downPayment,
termOfLoan: e.target.termOfLoan,
annualInterestRate: e.target.annualInterestRate
});
};
handleCostChange = () => {
const principal = this.state.houseCost - this.state.downPayment
const percentageRate = this.state.annualInterestRate / 1200
const lengthOfLoan = 12 * this.state.termOfLoan
this.setState(
prevState => ({
cost: (prevState.cost = (principal * percentageRate) / (1 - (Math.pow((1 + percentageRate) , lengthOfLoan * -1)))).toString()
});
);
};
render() {
return (
<div className="App">
<Counter
cost={this.state.cost}
houseCost={this.state.houseCost}
downPayment={this.state.downPayment}
termOfLoan={this.state.termOfLoan}
annualInterestRate={this.state.annualInterestRate}
changeCost={this.handleCostChange}
handleChange={this.handleChange}
/>
</div>
);
}
}
export default App;
Counter.js file
import React from 'react';
const Counter = (props) => {
return (
<div className="counter">
<input
type="number"
placeholder="House Cost"
onChange={(e) => props.handleChange(e)}>
</input>
<input
type="number"
placeholder="Down Payment"
onChange={(e) => props.handleChange(e)}>
</input>
<input
type="number"
placeholder="Mortgage Period (years)"
onChange={(e) => props.handleChange(e)}>
</input>
<input
type="number"
placeholder="Interest Rate"
onChange={(e) => props.handleChange(e)}>
</input>
<button
className="counter-action"
onClick={props.changeCost}
>Calculate
</button>
<span className="counter-score">{ props.cost }</span>
</div>
);
}
export default Counter;
javascript reactjs
javascript reactjs
edited Nov 5 at 22:00


hypnagogia
649
649
asked Nov 5 at 19:39
Sonya T
1141214
1141214
Theinput
tags should be self-closing tags.
– hypnagogia
Nov 5 at 20:54
add a comment |
Theinput
tags should be self-closing tags.
– hypnagogia
Nov 5 at 20:54
The
input
tags should be self-closing tags.– hypnagogia
Nov 5 at 20:54
The
input
tags should be self-closing tags.– hypnagogia
Nov 5 at 20:54
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
accepted
Try this
handleCostChange = () => {
const { houseCost, downPayment, annualInterestRate, termOfLoan } = this.state;
const principal = houseCost - downPayment
const percentageRate = annualInterestRate / 1200
const lengthOfLoan = 12 * termOfLoan;
const cost = (principal * percentageRate) / (1 - (Math.pow((1 + percentageRate) , lengthOfLoan * -1))).toString();
this.setState({
cost
})
}
Also you need further changes. Create individual event handlers and set the value using event.target.value
handleCostChange = (e) => {
this.setState({
houseCost: e.target.value,
});
}
handleDownPayment = (e) => {
this.setState({
downPayment: e.target.value,
});
}
handleannualInterestRate = (e) => {
this.setState({
annualInterestRate : e.target.value,
});
}
handleTermOfLoan = (e) => {
this.setState({
termOfLoan: e.target.value,
});
}
And pass down all event handlers
<Counter
cost={this.state.cost}
houseCost={this.state.houseCost}
downPayment={this.state.downPayment}
termOfLoan={this.state.termOfLoan}
annualInterestRate={this.state.annualInterestRate}
handleCostChange={this.handleCostChange}
handleTermOfLoan ={this.handleTermOfLoan}
handleannualInterestRate={this.handleannualInterestRate}
handleDownPayment={this.handleDownPayment}
/>
Now in Counter.js pass values as value prop to input elements along with individual event handler functions
<input type="number" placeholder="House Cost" onChange={(e) => props.handleCostChange(e)} value={props.houseCost}></input>
<input type="number" placeholder="Down Payment" onChange={(e) => props.handleDownPayment(e)} value={props.downPayment}></input>
<input type="number" placeholder="Mortgage Period (years)" onChange={(e) => props.handleTermOfLoan(e)} value={props.termOfLoan}></input>
<input type="number" placeholder="Interest Rate" onChange={(e) => props.handleannualInterestRate(e)} value={annualInterestRate}></input>
Please excuse me for typo errors because I am answering on my mobile
Here is the error that I get - Received NaN for thechildren
attribute. If this is expected, cast the value to a string.
– Sonya T
Nov 5 at 20:00
I think that it doesn't see the value of the number inputs
– Sonya T
Nov 5 at 20:01
You can try my answer now
– Think-Twice
Nov 5 at 20:14
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
Try this
handleCostChange = () => {
const { houseCost, downPayment, annualInterestRate, termOfLoan } = this.state;
const principal = houseCost - downPayment
const percentageRate = annualInterestRate / 1200
const lengthOfLoan = 12 * termOfLoan;
const cost = (principal * percentageRate) / (1 - (Math.pow((1 + percentageRate) , lengthOfLoan * -1))).toString();
this.setState({
cost
})
}
Also you need further changes. Create individual event handlers and set the value using event.target.value
handleCostChange = (e) => {
this.setState({
houseCost: e.target.value,
});
}
handleDownPayment = (e) => {
this.setState({
downPayment: e.target.value,
});
}
handleannualInterestRate = (e) => {
this.setState({
annualInterestRate : e.target.value,
});
}
handleTermOfLoan = (e) => {
this.setState({
termOfLoan: e.target.value,
});
}
And pass down all event handlers
<Counter
cost={this.state.cost}
houseCost={this.state.houseCost}
downPayment={this.state.downPayment}
termOfLoan={this.state.termOfLoan}
annualInterestRate={this.state.annualInterestRate}
handleCostChange={this.handleCostChange}
handleTermOfLoan ={this.handleTermOfLoan}
handleannualInterestRate={this.handleannualInterestRate}
handleDownPayment={this.handleDownPayment}
/>
Now in Counter.js pass values as value prop to input elements along with individual event handler functions
<input type="number" placeholder="House Cost" onChange={(e) => props.handleCostChange(e)} value={props.houseCost}></input>
<input type="number" placeholder="Down Payment" onChange={(e) => props.handleDownPayment(e)} value={props.downPayment}></input>
<input type="number" placeholder="Mortgage Period (years)" onChange={(e) => props.handleTermOfLoan(e)} value={props.termOfLoan}></input>
<input type="number" placeholder="Interest Rate" onChange={(e) => props.handleannualInterestRate(e)} value={annualInterestRate}></input>
Please excuse me for typo errors because I am answering on my mobile
Here is the error that I get - Received NaN for thechildren
attribute. If this is expected, cast the value to a string.
– Sonya T
Nov 5 at 20:00
I think that it doesn't see the value of the number inputs
– Sonya T
Nov 5 at 20:01
You can try my answer now
– Think-Twice
Nov 5 at 20:14
add a comment |
up vote
0
down vote
accepted
Try this
handleCostChange = () => {
const { houseCost, downPayment, annualInterestRate, termOfLoan } = this.state;
const principal = houseCost - downPayment
const percentageRate = annualInterestRate / 1200
const lengthOfLoan = 12 * termOfLoan;
const cost = (principal * percentageRate) / (1 - (Math.pow((1 + percentageRate) , lengthOfLoan * -1))).toString();
this.setState({
cost
})
}
Also you need further changes. Create individual event handlers and set the value using event.target.value
handleCostChange = (e) => {
this.setState({
houseCost: e.target.value,
});
}
handleDownPayment = (e) => {
this.setState({
downPayment: e.target.value,
});
}
handleannualInterestRate = (e) => {
this.setState({
annualInterestRate : e.target.value,
});
}
handleTermOfLoan = (e) => {
this.setState({
termOfLoan: e.target.value,
});
}
And pass down all event handlers
<Counter
cost={this.state.cost}
houseCost={this.state.houseCost}
downPayment={this.state.downPayment}
termOfLoan={this.state.termOfLoan}
annualInterestRate={this.state.annualInterestRate}
handleCostChange={this.handleCostChange}
handleTermOfLoan ={this.handleTermOfLoan}
handleannualInterestRate={this.handleannualInterestRate}
handleDownPayment={this.handleDownPayment}
/>
Now in Counter.js pass values as value prop to input elements along with individual event handler functions
<input type="number" placeholder="House Cost" onChange={(e) => props.handleCostChange(e)} value={props.houseCost}></input>
<input type="number" placeholder="Down Payment" onChange={(e) => props.handleDownPayment(e)} value={props.downPayment}></input>
<input type="number" placeholder="Mortgage Period (years)" onChange={(e) => props.handleTermOfLoan(e)} value={props.termOfLoan}></input>
<input type="number" placeholder="Interest Rate" onChange={(e) => props.handleannualInterestRate(e)} value={annualInterestRate}></input>
Please excuse me for typo errors because I am answering on my mobile
Here is the error that I get - Received NaN for thechildren
attribute. If this is expected, cast the value to a string.
– Sonya T
Nov 5 at 20:00
I think that it doesn't see the value of the number inputs
– Sonya T
Nov 5 at 20:01
You can try my answer now
– Think-Twice
Nov 5 at 20:14
add a comment |
up vote
0
down vote
accepted
up vote
0
down vote
accepted
Try this
handleCostChange = () => {
const { houseCost, downPayment, annualInterestRate, termOfLoan } = this.state;
const principal = houseCost - downPayment
const percentageRate = annualInterestRate / 1200
const lengthOfLoan = 12 * termOfLoan;
const cost = (principal * percentageRate) / (1 - (Math.pow((1 + percentageRate) , lengthOfLoan * -1))).toString();
this.setState({
cost
})
}
Also you need further changes. Create individual event handlers and set the value using event.target.value
handleCostChange = (e) => {
this.setState({
houseCost: e.target.value,
});
}
handleDownPayment = (e) => {
this.setState({
downPayment: e.target.value,
});
}
handleannualInterestRate = (e) => {
this.setState({
annualInterestRate : e.target.value,
});
}
handleTermOfLoan = (e) => {
this.setState({
termOfLoan: e.target.value,
});
}
And pass down all event handlers
<Counter
cost={this.state.cost}
houseCost={this.state.houseCost}
downPayment={this.state.downPayment}
termOfLoan={this.state.termOfLoan}
annualInterestRate={this.state.annualInterestRate}
handleCostChange={this.handleCostChange}
handleTermOfLoan ={this.handleTermOfLoan}
handleannualInterestRate={this.handleannualInterestRate}
handleDownPayment={this.handleDownPayment}
/>
Now in Counter.js pass values as value prop to input elements along with individual event handler functions
<input type="number" placeholder="House Cost" onChange={(e) => props.handleCostChange(e)} value={props.houseCost}></input>
<input type="number" placeholder="Down Payment" onChange={(e) => props.handleDownPayment(e)} value={props.downPayment}></input>
<input type="number" placeholder="Mortgage Period (years)" onChange={(e) => props.handleTermOfLoan(e)} value={props.termOfLoan}></input>
<input type="number" placeholder="Interest Rate" onChange={(e) => props.handleannualInterestRate(e)} value={annualInterestRate}></input>
Please excuse me for typo errors because I am answering on my mobile
Try this
handleCostChange = () => {
const { houseCost, downPayment, annualInterestRate, termOfLoan } = this.state;
const principal = houseCost - downPayment
const percentageRate = annualInterestRate / 1200
const lengthOfLoan = 12 * termOfLoan;
const cost = (principal * percentageRate) / (1 - (Math.pow((1 + percentageRate) , lengthOfLoan * -1))).toString();
this.setState({
cost
})
}
Also you need further changes. Create individual event handlers and set the value using event.target.value
handleCostChange = (e) => {
this.setState({
houseCost: e.target.value,
});
}
handleDownPayment = (e) => {
this.setState({
downPayment: e.target.value,
});
}
handleannualInterestRate = (e) => {
this.setState({
annualInterestRate : e.target.value,
});
}
handleTermOfLoan = (e) => {
this.setState({
termOfLoan: e.target.value,
});
}
And pass down all event handlers
<Counter
cost={this.state.cost}
houseCost={this.state.houseCost}
downPayment={this.state.downPayment}
termOfLoan={this.state.termOfLoan}
annualInterestRate={this.state.annualInterestRate}
handleCostChange={this.handleCostChange}
handleTermOfLoan ={this.handleTermOfLoan}
handleannualInterestRate={this.handleannualInterestRate}
handleDownPayment={this.handleDownPayment}
/>
Now in Counter.js pass values as value prop to input elements along with individual event handler functions
<input type="number" placeholder="House Cost" onChange={(e) => props.handleCostChange(e)} value={props.houseCost}></input>
<input type="number" placeholder="Down Payment" onChange={(e) => props.handleDownPayment(e)} value={props.downPayment}></input>
<input type="number" placeholder="Mortgage Period (years)" onChange={(e) => props.handleTermOfLoan(e)} value={props.termOfLoan}></input>
<input type="number" placeholder="Interest Rate" onChange={(e) => props.handleannualInterestRate(e)} value={annualInterestRate}></input>
Please excuse me for typo errors because I am answering on my mobile
edited Nov 5 at 20:09
answered Nov 5 at 19:55


Think-Twice
6,3471937
6,3471937
Here is the error that I get - Received NaN for thechildren
attribute. If this is expected, cast the value to a string.
– Sonya T
Nov 5 at 20:00
I think that it doesn't see the value of the number inputs
– Sonya T
Nov 5 at 20:01
You can try my answer now
– Think-Twice
Nov 5 at 20:14
add a comment |
Here is the error that I get - Received NaN for thechildren
attribute. If this is expected, cast the value to a string.
– Sonya T
Nov 5 at 20:00
I think that it doesn't see the value of the number inputs
– Sonya T
Nov 5 at 20:01
You can try my answer now
– Think-Twice
Nov 5 at 20:14
Here is the error that I get - Received NaN for the
children
attribute. If this is expected, cast the value to a string.– Sonya T
Nov 5 at 20:00
Here is the error that I get - Received NaN for the
children
attribute. If this is expected, cast the value to a string.– Sonya T
Nov 5 at 20:00
I think that it doesn't see the value of the number inputs
– Sonya T
Nov 5 at 20:01
I think that it doesn't see the value of the number inputs
– Sonya T
Nov 5 at 20:01
You can try my answer now
– Think-Twice
Nov 5 at 20:14
You can try my answer now
– Think-Twice
Nov 5 at 20:14
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53161101%2freact-function-mortgage-calculator-returns-nan%23new-answer', 'question_page');
}
);
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
ckVO,ViH9mjo bdaa2KaIlEo 0iJYsslXJP 82Q dGWiAH,tYYLRKdJk,z0T7CMo7rnIrs9bsE Tn4s41KrcqCm
The
input
tags should be self-closing tags.– hypnagogia
Nov 5 at 20:54