Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Get the SUM of all inputted numbers in useState

I am currently adding the semiMonthlyRate, HolidayPay, SpecialHolidayPay, NightDiff and the overtimePay but the result is not the one that i’ve expected, all the inputed data I’ve put it in useState since the exercise doesn’t need a database.

this is the link of my project.
https://codesandbox.io/s/plus-minus-button-forked-e6wrf?file=/src/App.js

i’ve try to code this but it was not the expected output, I’ve used useEffect hook.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

setTotalEarnings(semiMonthlyRate + holidayPay + specialHolidayPay + calculateNightDiff + calculateOvertime);

>Solution :

Change your code where you do addition to this

      parseFloat(semiMonthlyRate) + parseFloat(holidayPay) + parseFloat(specialHoliday) + parseFloat(nightDiff) + parseFloat(overtime)

By Default the values of text input are treated as string so need to convert them. If you want to avoid this simply use <input type="number">, for taking input of numerical values

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading