'How to change "/" in the first word with function if it has 3 words and 2 words are invalid

I have 3 variables

let oneWord = "offering" 
let twoWord = "test_offering"  
let threeWord = "loan_offering_data"

I want to make where if the value of TwoWord is executed/displayed as test-offering and suppose the value of threeWord is executed/displayed it becomes loan/offering-data

how to make a function to meet the above criteria dynamic?



Solution 1:[1]

function reformat(word) {
    const wds=word.split("_");
    if (wds.length ===1) return word;
    if (wds.length ===2) return wds.join("-");
    if (wds.length ===3) return `${wds[0]}/${wds[1]}-${wds[2]}`;
    throw new Error("too many words");
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Arnon Rotem-Gal-Oz