0

I've been sitting on this for a few days now. I'm trying to pass through values from another function into a function to complete my calculations. The calculations are update successfully on the UI using (document.getElementById("elementId").textContent = and passing through an object here)and a user is supposed to select a button to further break down the calculations by month, week ect... The button selection works however it does not see the value and I'm left with NaN.

index.js

const timeFrame = (payPeriod) => {
    // console.log("HERE: ", payPeriod.target.name)
    let payPeriodRate;
    switch (payPeriod.target.name) {
        case 'year':
            payPeriodRate= updateValuesByPeriod (1)
            break;
        case 'month':
            payPeriodRate = updateValuesByPeriod(12);
            console.log ("month:", payPeriodRate)
            break;
        case 'biweekly':
            payPeriodRate = updateValuesByPeriod(26);
             // console.log ("biweekly:", payPeriodRate)
            break;         
        case 'week':
            payPeriodRate = updateValuesByPeriod(52);
            // console.log ("week:", payPeriodRate)
            break;
      
        case 'day':
            payPeriodRate = updateValuesByPeriod(260);
            // console.log ("day:", payPeriodRate)
            break;
        case 'hour':
            payPeriodRate = updateValuesByPeriod(1950);
            // console.log ("hour:", payPeriodRate)
            break;
    }
}

method 1:

export const updateValuesByPeriod = (divisor) => {
        console.log ("updateValuesByPeriod:", divisor
    
        document.getElementById("salaryValue").textContent = document.getElementById("salaryValue").textContent /  divisor

method 2:

const updateValuesByPeriod = (divisor) => {
    console.log ("updateValuesByPeriod:", divisor)
    let uiValues = calculateTax();

    document.getElementById("salaryValue").textContent = `- $${uiValues.grossIncome}` /  divisor
Reggie
  • 1
  • 1
  • 3
    I would imagine this is related to `textContent` being a string rather than a number in both your examples. (JS will normally do the conversion for you, but a `NaN` result definitely feels like perhaps the string isn't a valid number) – DBS Nov 16 '22 at 15:05
  • tip, maintain a diff var then grabbing a string from the dom, if dev adds a space your need also add trim() and parseInt etc, gets messy, just create a new var to hold value – Lawrence Cherone Nov 16 '22 at 15:07
  • Js uses [type coercion](https://developer.mozilla.org/en-US/docs/Glossary/Type_coercion). This may not do what you expect it to do. If you need a type, you should explicitly convert it, not rely on coercion – Liam Nov 16 '22 at 15:09

0 Answers0