0

I am getting an error at the function declaration

Parsing error : Unexpected

Here is my code:

class Item extends Component {
    function changeNumberFormat(number, decimals, recursiveCall) { //Parsing error : Unexpected Error
        const decimalPoints = decimals || 2;
        const noOfLakhs = number / 100000;
        let displayStr;
        let isPlural;

        // Rounds off digits to decimalPoints decimal places
        function roundOf(integer) {
            return +integer.toLocaleString(undefined, {
                minimumFractionDigits: decimalPoints,
                maximumFractionDigits: decimalPoints,
            });
        }

        if (noOfLakhs >= 1 && noOfLakhs <= 99) {
            const lakhs = roundOf(noOfLakhs);
            isPlural = lakhs > 1 && !recursiveCall;
            displayStr = `${lakhs} Lakh${isPlural ? 's' : ''}`;
        } else if (noOfLakhs >= 100) {
            const crores = roundOf(noOfLakhs / 100);
            const crorePrefix = crores >= 100000 ? changeNumberFormat(crores, decimals, true) : crores;
            isPlural = crores > 1 && !recursiveCall;
            displayStr = `${crorePrefix} Crore${isPlural ? 's' : ''}`;
        } else {
            displayStr = roundOf(+number);
        }

        return displayStr;
    }
    ...
    }

It looks like I am missing something with the syntax. What am I doing wrong?

3
  • 1
    When declaring a method in a class declaration you don't use the function keyword. Commented Nov 30, 2019 at 12:41
  • If I don't use the function keyword, it says the changeNumberFormat method is not defined. Commented Nov 30, 2019 at 12:43
  • 2
    When you call the method recursively, you have to us this.changeNumberFormat() Commented Nov 30, 2019 at 12:46

2 Answers 2

1

You may change your function to a method like this way

changeNumberFormat(number, decimals, recursiveCall) { ... }

or

changeNumberFormat = (number, decimals, recursiveCall)=> { ... }
Sign up to request clarification or add additional context in comments.

Comments

1

youa are trying to declare function inside a react component. Better would be to declare it outside the component.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.