React function mortgage calculator returns NAN











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;









share|improve this question
























  • The input tags should be self-closing tags.
    – hypnagogia
    Nov 5 at 20:54















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;









share|improve this question
























  • The input tags should be self-closing tags.
    – hypnagogia
    Nov 5 at 20:54













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;









share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 5 at 22:00









hypnagogia

649




649










asked Nov 5 at 19:39









Sonya T

1141214




1141214












  • 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
















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












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






share|improve this answer























  • 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










  • You can try my answer now
    – Think-Twice
    Nov 5 at 20:14











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















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
































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






share|improve this answer























  • 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










  • You can try my answer now
    – Think-Twice
    Nov 5 at 20:14















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






share|improve this answer























  • 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










  • You can try my answer now
    – Think-Twice
    Nov 5 at 20:14













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






share|improve this answer














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







share|improve this answer














share|improve this answer



share|improve this answer








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 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










  • 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












  • 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


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














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




















































































這個網誌中的熱門文章

Tangent Lines Diagram Along Smooth Curve

Yusuf al-Mu'taman ibn Hud

Zucchini