+ 2
Getting nan while using parseFloat and number with prompt in js
Why parseFloat with prompt outputs nan in js? Here's the ex... // Using parseFloat with prompt const weightInput = prompt("Enter your weight in kg:"); const parsedWeightFloat = parseFloat(weightInput); console.log("Parsed weight using parseFloat:", parsedWeightFloat); // Using Number with prompt const heightInput = prompt("Enter your height in cm:"); const parsedHeightNumber = Number(heightInput); console.log("Parsed height using Number:", parsedHeightNumber); // It outputs nan when giving input as strings like "5" etc , parseFloat should check if that string is convertable instead of nan.
13 ответов
+ 1
let f = ' 5.5 '
let f1 = ' "5.5" '
console.log(parseFloat(f)) //5.5
console.log(parseFloat(f1))//NaN
//prompt adds " " to your prompt input. So there is no need to add it yourself. To see the raw string prompt() returns:
console.log(JSON.stringify(prompt()))
+ 2
I don't get nan.
It seems to work fine.
I testet integer and float.
+ 2
Bob_Li yes but parseFloat function is designed to handle strings representing numbers, so shouldn't it should parse the numeric part of the string?
+ 1
"It's strange because I'm still getting NaN. If only there were a way to send a screenshot or some output, I would."
+ 1
Wow, I never imagined a user would enter quoted numbers or text, unless they r told to do so.
This was pretty interesting.
0
You can add a screenshot to your feed. You find your feed in the community section.
0
Niraj Kumar
are you typing
"5"
in the prompt?
you should not add the quotation marks. prompt input are already string by default.
0
This loop will continue prompting the user for input until a valid number is entered.
you can add additional validation logic to check if the input is NaN and prompt the user again until they provide a valid input
0
Hii
0
Cool guy python
0
https://sololearn.com/compiler-playground/WUeqDVFPs320/?ref=app
https://sololearn.com/compiler-playground/W3Dqey6X8Jo5/?ref=app
https://sololearn.com/compiler-playground/w7Mb6WIa8cxR/?ref=app
https://sololearn.com/compiler-playground/WiMQSZVahKht/?ref=app
https://sololearn.com/compiler-playground/WJdKtD0j5lLk/?ref=app
https://sololearn.com/compiler-playground/W2Ptc5olf3rZ/?ref=app
https://sololearn.com/compiler-playground/WC6HKPr6Izl1/?ref=app
https://sololearn.com/compiler-playground/cbrhDqfT1rcm/?ref=app