프로젝트의 input에 0이나 음수 값을 입력하면 다음과 같은 오류 화면이 뜬다.

// Results.jsx
export default function Results({ input }) {
const results = [];
calculateInvestmentResults(input, results);
const initialInvestment =
results[0].valueEndOfYear -
results[0].interest -
results[0].annualInvestment;
// ...
}
calculateInvestmentResults함수의 인수로 전달된다.// calculateInvestmentResults
export function calculateInvestmentResults(
{ initialInvestment, annualInvestment, expectedReturn, duration },
results
) {
// results
let investmentValue = initialInvestment;
for (let i = 0; i < duration; i++) {
const interestEarnedInYear = investmentValue * (expectedReturn / 100);
investmentValue += interestEarnedInYear + annualInvestment;
results.push({
// results에 값들이 Push 된다.
year: i + 1, // year identifier
interest: interestEarnedInYear, // the amount of interest earned in this year
valueEndOfYear: investmentValue, // investment value at end of year
annualInvestment: annualInvestment, // investment added in this year
});
}
}
results.push()는 for문에서 발생!results[0]가 undefined라는 것을 알 수 있다.export default function Results({ input }) {
const results = [];
calculateInvestmentResults(input, results);
if (results.length === 0) {
// results가 빈 객체라면..
return <p>Invalid input data provided.</p>;
}
const initialInvestment =
results[0].valueEndOfYear -
results[0].interest -
results[0].annualInvestment;
//...
}


calculateInvestmentResults()에 오류는 없는 것으로 보인다.

investmentValue는 문자열이고 interestEarnedInYear,annualInvestmen는 숫자인 것을 알 수 있다."1000"+200을 하면 결과는 "1000200"이 될 것이다.function App() {
function handleChange(inputIdentifier, newValue) {
setUserInput((prevUserInput) => {
return {
...prevUserInput,
[inputIdentifier]: +newValue, // 새로 입력하는 값을 문자열->숫자로 변환
};
});
}
}
import { calculateInvestmentResults, formatter } from "../util/investment.js";
const results = [];
export default function Results({ input }) {}

import { StrictMode } from "react"; // strict mode는 리액트에서 import하는 컴포넌트이므로 index.jsx에 작성.
import ReactDOM from "react-dom/client";
import App from "./App.jsx";
import "./index.css";
ReactDOM.createRoot(document.getElementById("root")).render(
<StrictMode>
<App />
</StrictMode>
);
// 혹은 더 깉은 곳에서 엄격모드를 사용할 수 있다.
// App.jsx
function App() {
return (
<>
<Header />
<UserInput userInput={userInput} onChange={handleChange} />
<StrictMode>
<Results input={userInput} />
</StrictMode>
</>
);
}
import { calculateInvestmentResults, formatter } from "../util/investment.js";
const results = [];
export default function Results({ input }) {}
