number.charAt() is not a function

Tell us what’s happening:
I completed the challenge in visual studio code, however when I copy and pasted it over I get this error ‘TypeError: number.charAt is not a function’. It works completely fine on Visual Studio, the only difference is on VS I entered the number with a prompt to test where as on freecodecamp the numbers are entered automatically. Either way that has nothing to do with the error.

Your code so far

WARNING

The challenge seed code and/or your solution exceeded the maximum length we can port over from the challenge.

You will need to take an additional step here so the code you wrote presents in an easy to read format.

Please copy/paste all the editor code showing in the challenge from where you just linked.

function convertToRoman(num) {

  var number = num;

  var singleDigits = [];
            var doubleDigits = ["0"];
            var tripleDigits = ["0", "0"];
            var quadDigits = ["0", "0", "0"];

            var getSingleDigit;
            var getDoubleDigit;
            var getTripleDigit; 
            var getQuadDigit;

            if (number > 0 && number < 10) {
                getSingleDigit = number.charAt(0);
                singleDigits.unshift(getSingleDigit);

                getSingleDigit = singleDigits.join('');

                formRomanNumeral(getSingleDigit);            
            }
            else if (number >= 10 && number < 100) {
                getSingleDigit = number.charAt(1);
                getDoubleDigit = number.charAt(0);

                singleDigits.unshift(getSingleDigit);
                getSingleDigit = singleDigits.join(''); 

                doubleDigits.unshift(getDoubleDigit);
                getDoubleDigit = doubleDigits.join(''); 

                formRomanNumeral(getSingleDigit, getDoubleDigit);
            }
            else if (number >= 100 && number < 1000) {
                getSingleDigit = number.charAt(2);
                getDoubleDigit = number.charAt(1);
                getTripleDigit = number.charAt(0);

                singleDigits.unshift(getSingleDigit);
                getSingleDigit = singleDigits.join(''); 

                doubleDigits.unshift(getDoubleDigit);
                getDoubleDigit = doubleDigits.join('');

                tripleDigits.unshift(getTripleDigit);
                getTripleDigit = tripleDigits.join('');

                formRomanNumeral(getSingleDigit, getDoubleDigit, getTripleDigit);
            }
            else if (number >= 1000) {
                getSingleDigit = number.charAt(3);
                getDoubleDigit = number.charAt(2);
                getTripleDigit = number.charAt(1);
                getQuadDigit = number.charAt(0);

                singleDigits.unshift(getSingleDigit);
                getSingleDigit = singleDigits.join(''); 

                doubleDigits.unshift(getDoubleDigit);
                getDoubleDigit = doubleDigits.join('');

                tripleDigits.unshift(getTripleDigit);
                getTripleDigit = tripleDigits.join('');

                quadDigits.unshift(getQuadDigit);
                getQuadDigit = quadDigits.join('');

                formRomanNumeral(getSingleDigit, getDoubleDigit, getTripleDigit, getQuadDigit);

            }
            else {
                console.log("We cant convert that high yet!");
            }

            
            console.log(getSingleDigit);
            console.log(getDoubleDigit);
            console.log(getTripleDigit);
 //return num;
}

function formRomanNumeral(one, ten, hundred, thousand) {

        const romanNumeralOnesCombinations = {
            "1": "I",  "2": "II",  "3": "III", "4": "IV",  "5": "V",  "6": "VI",  "7": "VII",  "8": "VIII",  "9": "IX"            
        };
        const romanNumeralTensCombinations = {
            "10": "X",  "20": "XX",  "30": "XXX",  "40": "XL",  "50": "L",  "60": "LX",  "70": "LXX",  "80": "LXXX",  "90": "XC"
        };
        const romanNumeralHundredsCombinations = {
            "100": "C",  "200": "CC",  "300": "CCC",  "400": "CD",  "500": "D",  "600": "DC",  "700": "DCC",  "800": "DCCC",  "900": "CM"            
        };
        const romanNumeralThousandsCombinations = {
            "1000": "M"
        };

        let formRomanArray = [];
        let formRomanString;

        if (thousand != null) {
            formRomanArray.unshift(romanNumeralOnesCombinations[one]);
            formRomanArray.unshift(romanNumeralTensCombinations[ten]);
            formRomanArray.unshift(romanNumeralHundredsCombinations[hundred]);
            formRomanArray.unshift(romanNumeralThousandsCombinations[thousand]);

            formRomanString = formRomanArray.join('');

            console.log(formRomanString);

        }
        else if (hundred != null) {
            formRomanArray.unshift(romanNumeralOnesCombinations[one]);
            formRomanArray.unshift(romanNumeralTensCombinations[ten]);
            formRomanArray.unshift(romanNumeralHundredsCombinations[hundred]);

            formRomanString = formRomanArray.join('');

            console.log(formRomanString);
        }
        else if (ten != null) {
            formRomanArray.unshift(romanNumeralOnesCombinations[one]);
            formRomanArray.unshift(romanNumeralTensCombinations[ten]);

            formRomanString = formRomanArray.join('');

            console.log(formRomanString);
        }
        else if (one != null) {
            formRomanArray.unshift(romanNumeralOnesCombinations[one]);

            formRomanString = formRomanArray.join('');

            console.log(formRomanString);
        }
        else {
            console.log("What happened here");
        }

        

    }

convertToRoman(36);

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.45 Safari/537.36

Challenge: Roman Numeral Converter

Link to the challenge:

Well, the charAt method is on the String prototype. Are you sure that the number variable is a string?

Quite the contrary, I think.

1 Like

Yeah I think you might be right. Originally number was a string because it was forming from this line in visual studio. let number = prompt(“enter number”, “”);
but after copy pasting over its not longer the case because number is defined in the function

1 Like