Roman Numeral array return undefined

const input = document.getElementById("number")
const button = document.getElementById("convert-btn")
const output = document.getElementById("output")
let array = []


button.addEventListener("click", () => {
    
    if(!input.value){
        output.textContent = "Please enter a valid number";
    }else if(input.value < 0){
        output.textContent = "Please enter number greater than or equal to 1"
    }else if(input.value >= 4000){
        output.textContent = "Please enter a number less than or equal to 3999"
    }else{ 
        //this code, here //
        convert(input.value)
        output.textContent = mapJoin(array).join("")
    }

    input.value = "";
    array = [];
})

const convert = (input) => {
    
    const list = [1000,900,500,400,100,90,50,40,10,9,5,4,1]

    if(input === 0){
        return array;
    }else{
        const tolak = list.filter(num => num <= input);
        array.push(tolak[0])
        convert(input - tolak[0])
    }
}

function mapJoin(array){
    return array.map(
        numToRoman =>{
            switch(numToRoman){
                case 1000:
                    return "M"
                    break;
                case 900:
                    return "CM"
                    break;
                case 500: 
                    return "D"
                    break;
                case 400:
                    return "CD"
                    break;
                case 100:
                    return "C"
                    break;
                case 90: 
                    return "XC"
                    break;
                case 50:
                    return "L"
                    break;
                case 40:
                    return "XL"
                    break;
                case 10:
                    return "X"
                    break;
                case 9:
                    return "IX"
                    break;
                case 5:
                    return "V"
                    break;
                case 4:
                    return "IV"
                    break;
                case 1:
                    return "I"
                    break;
            }
        }
    )
}

why my return value of convert(input.value) is undefined?

supposedly i wanted to directly use this syntax

but the console say my return value is undefined

output.textContent = mapJoin(convert(input.value)).join("")

You really should not use this global variable.

Honestly, this isn’t a great problem for recursion

My initial suggestion would be to employ console.log statements at various places

Semicolons? I can’t see several of it.

Semicolons are mostly optional, as they are automatically inserted.

MDN: Automatic semicolon insertion

For learners, I would suggest using semicolons until they are comfortable enough debugging the edge cases.

For code collaboration, I think it is best to required them, but that is up for debate and is just a style and personal preference.

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