Trying to parse but returns NAN

I keep starting and stopping with trying to refresh myself with this. I have the buttons which hold a value of “.5”, “.10”, “.15” and so on. When the user selects a tip amount I am trying to see what gets returned right now. The value attribute returns text, and so to make it a number I would need to parse it. However, when I try to parse it and then console.log to see what comes back I get NAN

Pen:

When in doubt, log it out. See where the problem is.

When I add in some log statements:

const calculateTotal = () => {
    cal.addEventListener("click", () => {
        const bill = billTotal.value;
        console.log('bill =', bill)
        getTip();
        const numPeople = people.value;
        console.log('numPeople =', numPeople)
        const tip = (bill * getTip);
        console.log('tip =', tip)
        const total = bill + tip;
        console.log('total =', total)
        const perPerson = total / numPeople;
        console.log('perPerson =', perPerson)
        tipA.innerHTML= tip;
      console.log(typeof tip);
    })
}

I see some values that don’t seem right. I would start there.

I havent checked that function yet. I will still need to do some parsing there, but just the get tip function at the beginning returns NAN. Before I even get to the calculate function I already get NAN

OK… but applying the same idea:

document.querySelectorAll(".percent").forEach((item) => {
    item.addEventListener("click", () => {
    percentValue = item.value;
    console.log('percentValue', percentValue)
    percentParse = parseInt(percentValue);
    console.log('percentParse', percentParse);
    })
})

Can you isolate the problem? How does parseInt work?

1 Like

Ahh after looking at the notes more carefully. So, if the first character can not be converted then it returns NAN. Of course, it is right in the name as well parseINT meaning it can only do whole numbers. I would need parseFloat or the Number method

1 Like

Yeah, and then do a little math, if you want a percentage.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.