VERY simple if / else if / else loop not working

Viewed 27

I am making a very simple Hook function to help convert Date objects into a DateTime format, and that includes turning the spelled out month into a number (like "Sep" to 9) The Hook I have takes in that 3 character string, and should spit out a number, but instead keeps returning nothing. This is because none of the if or `else if's are being hit,

export default function convertMonthToNumber(mon){
    console.log(mon, "MONTH")
    if (mon === "Jan"){
        return 1
    }
    else if (mon == "Feb"){
        return 2
    }
    if (mon == "Mar"){
        return 3
    }
    if (mon == "Apr"){
        return 4
    }
    if (mon == "May"){
        return 5
    }
    if (mon == "Jun"){
        return 6
    }
    if (mon == "Jul"){
        return 7
    }
    if (mon == "Aug"){
        return 8
    }
    if (mon == "Sep"){
        return 9
    }
    if (mon == "Oct"){
        return 10
    }
    if (mon == "Nov"){
        return 11
    }
    if (mon == "Dec"){
        return 12
    }
    else{
        console.log(mon, "MONTH")
        console.log("???")
    }
}

Which will print the following...

 LOG  Sep  MONTH
 LOG  Sep  MONTH
 LOG  ???

And call me a madman, but I firmly believe that "Sep" === "Sep" Anyone see what's going wrong?

1 Answers

Although there was no extra whitespace in the input since

console.log(`...${mon}...`)

printed

...Sep...

But it's possible that there was some direct value issue, since I get 'Sep' from the Date.toString() function, that I then used split() function on. Regardless, changing all of the mon == into mon.includes()

Related