Coming soon - Get a detailed view of why an account is flagged as spam!
view details

This post has been de-listed

It is no longer included in search results and normal feeds (front page, hot posts, subreddit posts, etc). It remains visible only via the author's post history.

1
JavaScript Function Returns NaN (API Call)
Post Flair (click to view more posts with a particular flair)
Post Body

Hello, so this is a Next.js app I'm building. I have two input fields and I'm converting units of crypto entered into the first input field and calculating a price. It works the other way around to, so if you enter 100 in the second input field, it gives how much of that crypto you would have.

I'm using the useState hook for the price and the useEffect hook for the API call.

The problem is that the last value entered always displays as NaN. For example, if I load the app, and enter 1 into the first input field, it will display the correct price in the second field but the 1 disappears and displays NaN.

I've narrowed it down to this function:

function handleInput(e) {
if (e.key === "Enter") {
e.preventDefault();

const inputOneValue = document.getElementById("inputOne").value;
const inputTwoValue = document.getElementById("inputTwo").value;

const convertedInputOneValue =
parseFloat(inputTwoValue) / parseFloat(price);
const convertedInputTwoValue =
parseFloat(inputOneValue) * parseFloat(price);

document.getElementById("inputOne").value =
convertedInputOneValue.toFixed(2);

document.getElementById("inputTwo").value =
convertedInputTwoValue.toFixed(2);
}
}

More specifically, I think it has to do with a line like const inputOneValue = document.getElementById("inputOne").value; because if I add || 0 to the end, the NaN stops displaying and the problem becomes a 0 displays instead of NaN.

I've spent hours on this, so any help would be greatly appreciated!

Author
Account Strength
100%
Account Age
10 years
Verified Email
Yes
Verified Flair
No
Total Karma
16,178
Link Karma
3,901
Comment Karma
12,189
Profile updated: 2 days ago
Posts updated: 1 year ago

Subreddit

Post Details

We try to extract some basic information from the post title. This is not always successful or accurate, please use your best judgement and compare these values to the post title and body for confirmation.
Posted
1 year ago