0

I want to write the two function inside RomanNumerals function

RomanNumerals.toRoman()
RomanNumberals.fromRoman()

which will return the Roman Number or decode the Roman Numerals. The code is the following as below

function RomanNumerals(x){
this.toRoman = function(x){
        var  roman = {M:1000,CM:900, D:500,CD:400,C:100,XC:90,L:50,XL:40,X:10,IX:9,V:5,IV:4,I:1 }
  
  var ans = '';
  while(number>0){
      for(a in roman){ 
          console.log(a,ans)
          if(roman[a]<=number)
          { ans += a; 
            number-=roman[a]; 
            console.log(a,ans,number)
            break;}
              
      }
  }
  return ans;
  }
  
this.fromRoman = function(x){
        let sum = 0, a;
        const romanNo = { I : 1, V : 5, X : 10, L : 50, C: 100, D : 500, M : 1000}
        const numbers = roman.split('')
        for(let i = 0; i < numbers.length; i++ ){
            if (romanNo[numbers[i]] < romanNo[numbers[i+1]])
            {
                sum += romanNo[numbers[i+1]] - romanNo[numbers[i]]
                i++;
            }
            else 
            {
                 sum+=romanNo[numbers[i]]
            }
        }
        return sum

    }

}

but it is still occouring the following error

TypeError: RomanNumerals.toRoman is not a function

I have looked up a few references similar to this post and followed the following link JavaScript Nested function the code syntax mentioned at the below

function Fizz(qux)
{
  this.buzz = function(){
    console.log( qux );
  };
}

still occur the following error as well

TypeError: Fizz.buzz is not a function

What am I missing here to solve this issue?

echo
  • 11
  • 2
  • `this` within `RomanNumerals` will not, in the normal course of things, ever refer to the `RomanNumerals` function, so `RomanNumerals.toRoman` won't refer to a function. Based on what you've shown, there's no need for `RomanNumerals` to be a function at all, just an object (`const RomanNumerals = { toRoman(x) { /*...*/}, fromRoman(x) { /*...*/ } };`), but it depends entirely on what you're trying to build. – T.J. Crowder Jan 13 '22 at 13:02
  • I followed the syntax structure of The fizz.buzz() code answered at the other topic. but it occur the "not a function" error also so is it the syntax problem or what should I change in that small code. – echo Jan 13 '22 at 13:10
  • From the answer with the example you refer to: *"The `Fizz` function is designed as a constructor so that, when run, it assigns a `buzz` function to the newly created object."* (It would have been better if the usage had been shown by the answer.) So you don't use it as `Fizz.buzz()`, you use it as `const obj = new Fizz(); obj.buzz();` (That would also work with your `RomanNumerals`.) – T.J. Crowder Jan 13 '22 at 13:16
  • 1
    Thank you very much for your time and efforts – echo Jan 13 '22 at 13:43

0 Answers0