Question

Error in Code

Hi Team I am trying to write a code in Javascript where if the number is 100 it should display hundred in words.

Please check my code it is showing some error and assist me how will I fix this and achieve this scenario.(check the statement higlighted in bold).

    changeToNumValid:function()

        {

            var num=this.get("UsrAmount");

         var ones = [

          "", "one", "two", "three", "four",

          "five", "six", "seven", "eight", "nine",

          "ten", "eleven", "twelve", "thirteen", "fourteen",

         "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"

          ];

  var tens = [

    "", "", "twenty", "thirty", "forty",

    "fifty", "sixty", "seventy", "eighty", "ninety"

  ];

  var numString = num.toString();

  if (num < 0) {var invalidMessage=this.get("Resources.Strings.Error") ;

       return {  

      invalidMessage: invalidMessage

                };

  }

  if (num === 0) {

      this.set("UsrWord","Zero");

  }

  //the case of 1 - 20

  if (num < 20) {

        this.set("UsrWord",ones[num]);

  }

  if (numString.length === 2) {

      var val=tens[numString[0]] + "" + ones[numString[1]];

        this.set("UsrWord",val);

  }

  //100 and more

  if (numString.length ===3) {

    if (numString[1] === "0" && numString[2] === "0")

    {  

        var val1= ones[numString[0]] + " hundred";

            this.set("UsrWord",val1);

    }

    else{

        var val2=ones[numString[0]] + " hundred and " + changeToNumValid(+(numString[1] + numString[2]));

      this.set("UsrWord",val2);

  }}

  if (numString.length === 4) {

    var end = +(numString[1] + numString[2] + numString[3]);

    if (end === 0) { var val3=ones[numString[0]] + "thousand";

         this.set("UsrWord",val3);

    }

    if (end < 100) {return ones[numString[0]] + "thousand and " + changeToNumValid(end);}

    var val4= ones[numString[0]] +  "thousand " + changeToNumValid(end);

    this.set("UsrWord",val4);

  }

 

        

},

            setValidationConfig :function(){

                    this.callParent(arguments);

                  this.addColumnValidator("UsrWord", this.changeToNumValid);

            }

Like 0

Like

9 comments

in your code (inside the changeToNum function), change all calls to changeToNum to this.changeToNum (note the "this." in front of the function name)

 

Ryan

Ryan Farley,

Hy Ryan,

please check I have made the changes and it got successfully saved but I am not able to deliver the exact output. It is showing output till 2 digit that is 21 twenty but not showing output for 324 three hundred twenty-four. Please check it and provide a solution

Also please check that I am not able to save the page It keep on loading how will I fix this :( ?

Bhumika Bisht,

 

You should receive the "Maximum call stack size exceeded" error message in the console when the setValidationConfig function is called since you are calling a function inside a function inside a function multiple times. It is better to rewrite the code excluding calling the changeNumToValid function inside the changeNumToValid function. Something like the code below (it is not a ready code but a part of it):

define("AccountPageV2", [], function() {
	return {
		entitySchemaName: "Account",
		attributes: {},
		modules: /**SCHEMA_MODULES*/{}/**SCHEMA_MODULES*/,
		details: /**SCHEMA_DETAILS*/{}/**SCHEMA_DETAILS*/,
		businessRules: /**SCHEMA_BUSINESS_RULES*/{}/**SCHEMA_BUSINESS_RULES*/,
		methods: {
			onEntityInitialized: function(){
			this.callParent(arguments);
			this.numToString();
		},
			numToString:function (){
			let numValue = this.get("UsrAmount");
			let stringBeforeFunctionValue;
			let stringAfterFunctionValue;
			let substrictionResult;
			let stringConcatTyPart;
			let stringConcatUnitPart;
			let numValueDivisionBy11Check;
			let stringConcatHundredsPart;
			let unitsArray = ["","one","two","three","four","five","six","seven","eight","nine","ten"];
			let elevenNineteen = ["eleven","twelwe","thirteen","fourteen","fiveteen","sixteen","seventeen",
								"eighteen","nineteen"];
			let tyArray = ["","","twenty","thirty","fourty","fifty","sixty","seventy","eighty","ninety"];
			stringBeforeFunctionValue = String(numValue);
    				if (numValue&lt;=10){
        				stringAfterFunctionValue = unitsArray[numValue].toString();
    					this.set("UsrWord",stringAfterFunctionValue);
    } else if (numValue &gt;10 &amp;&amp; numValue &lt; 20){
        substrictionResult = numValue-11;
        stringAfterFunctionValue = String(elevenNineteen[substrictionResult]);
        this.set("UsrWord",stringAfterFunctionValue);
    } else if (numValue&gt;=20 &amp;&amp; numValue&lt;=99){
        for (let i=2;i&lt;=9;i++){
            for (let j=1;j&lt;=9;j++){
                if (stringBeforeFunctionValue.indexOf(i)===0){
                    stringConcatTyPart = String(tyArray[i]);
                } else if (stringBeforeFunctionValue.indexOf(j)===1){
                    stringConcatUnitPart = String(unitsArray[j]);
                }
            }
        } if (numValue%11===0){
            numValueDivisionBy11Check = numValue/11;
            stringConcatTyPart = String(tyArray[numValueDivisionBy11Check]);
            stringConcatUnitPart = String(unitsArray[numValueDivisionBy11Check]);
        }
        stringAfterFunctionValue = stringConcatTyPart + " " + stringConcatUnitPart;
        this.set("UsrWord",stringAfterFunctionValue);
        }
 
        //hundreds
 
        else if (numValue&gt;=100 &amp;&amp; numValue &lt;= 999){
            for (let a=1;a&lt;=9;a++){
                 if (stringBeforeFunctionValue.indexOf(a)===0 &amp;&amp; numValue%100===0){
                     if (numValue/100===1){
                        stringConcatHundredsPart = `${String(unitsArray[a])} hundred`;
                        stringAfterFunctionValue = stringConcatHundredsPart;
                     } else {
                        stringConcatHundredsPart = `${String(unitsArray[a])} hundreds`;
                        stringAfterFunctionValue = stringConcatHundredsPart;
                     }
                    this.set("UsrWord",stringAfterFunctionValue);
                }
                else if (stringBeforeFunctionValue.indexOf(a)===0 &amp;&amp; numValue%100!==0){
                    if (a===1){
                    stringConcatHundredsPart = `${String(unitsArray[a])} hundred`;
                    for (let b=0;b&lt;=9;b++){
                        for (let c=1;c&lt;=9;c++){
                            if (stringBeforeFunctionValue.indexOf(b)===1){
                                stringConcatTyPart = String(tyArray[b]);
                            } else if (stringBeforeFunctionValue.indexOf(c)===2){
                                stringConcatUnitPart = String(unitsArray[c]);
                            } else if ((numValue%100)%11===0){
                                numValueDivisionBy11Check = (numValue%100)/11;
                                stringConcatTyPart = String(tyArray[numValueDivisionBy11Check]);
                                stringConcatUnitPart = String(unitsArray[numValueDivisionBy11Check]);
                            }
                            stringAfterFunctionValue =
                            stringConcatHundredsPart +" "+ stringConcatTyPart +" "+ stringConcatUnitPart;
                        }
                    }
                } else if (numValue%100 &gt; 11 &amp;&amp; numValue%100 &lt; 20){
                    stringConcatHundredsPart = `${String(unitsArray[a])} hundred`;
                    substrictionResult = numValue%100 - 11;
                    stringAfterFunctionValue = stringConcatHundredsPart + " " + elevenNineteen[substrictionResult];
                } 
                else {
                    stringConcatHundredsPart = `${String(unitsArray[a])} hundred`;
                    for (let d=0;d&lt;=9;d++){
                        for (let e=1;e&lt;=9;e++){
                            if (stringBeforeFunctionValue.indexOf(d)===1){
                                stringConcatTyPart = String(tyArray[d]);
                            } else if (stringBeforeFunctionValue.indexOf(e)===2){
                                stringConcatUnitPart = String(unitsArray[e]);
                            } else if ((numValue%100)%11===0){
                                numValueDivisionBy11Check = (numValue%100)/11;
                                stringConcatTyPart = String(tyArray[numValueDivisionBy11Check]);
                                stringConcatUnitPart = String(unitsArray[numValueDivisionBy11Check]);
                            }
                            stringAfterFunctionValue =
                            stringConcatHundredsPart + " " + stringConcatTyPart +" "+ stringConcatUnitPart;
                        }
                    }
                }
                this.set("UsrWord",stringAfterFunctionValue);
             }
        }
    }
}
		},
		dataModels: /**SCHEMA_DATA_MODELS*/{}/**SCHEMA_DATA_MODELS*/,
		diff: /**SCHEMA_DIFF*/[
			{
				"operation": "merge",
				"name": "AccountPhotoContainer",
				"values": {
					"layout": {
						"colSpan": 24,
						"rowSpan": 1,
						"column": 0,
						"row": 0
					}
				}
			},
			{
				"operation": "merge",
				"name": "AccountName",
				"values": {
					"layout": {
						"colSpan": 24,
						"rowSpan": 1,
						"column": 0,
						"row": 1
					}
				}
			},
			{
				"operation": "merge",
				"name": "AccountType",
				"values": {
					"layout": {
						"colSpan": 24,
						"rowSpan": 1,
						"column": 0,
						"row": 2
					}
				}
			},
			{
				"operation": "merge",
				"name": "AccountOwner",
				"values": {
					"layout": {
						"colSpan": 24,
						"rowSpan": 1,
						"column": 0,
						"row": 3
					}
				}
			},
			{
				"operation": "merge",
				"name": "AccountWeb",
				"values": {
					"layout": {
						"colSpan": 24,
						"rowSpan": 1,
						"column": 0,
						"row": 4
					}
				}
			},
			{
				"operation": "merge",
				"name": "AccountPhone",
				"values": {
					"layout": {
						"colSpan": 24,
						"rowSpan": 1,
						"column": 0,
						"row": 5
					}
				}
			},
			{
				"operation": "merge",
				"name": "NewAccountCategory",
				"values": {
					"layout": {
						"colSpan": 24,
						"rowSpan": 1,
						"column": 0,
						"row": 6
					}
				}
			},
			{
				"operation": "merge",
				"name": "AccountIndustry",
				"values": {
					"layout": {
						"colSpan": 24,
						"rowSpan": 1,
						"column": 0,
						"row": 7
					}
				}
			},
			{
				"operation": "merge",
				"name": "AccountPageGeneralTabContainer",
				"values": {
					"order": 0
				}
			},
			{
				"operation": "merge",
				"name": "AlternativeName",
				"values": {
					"layout": {
						"colSpan": 12,
						"rowSpan": 1,
						"column": 0,
						"row": 0
					}
				}
			},
			{
				"operation": "merge",
				"name": "Code",
				"values": {
					"layout": {
						"colSpan": 12,
						"rowSpan": 1,
						"column": 12,
						"row": 0
					}
				}
			},
			{
				"operation": "insert",
				"name": "UsrTeama4b5e38f-fb4d-426c-a03a-ef468c5ad35b",
				"values": {
					"layout": {
						"colSpan": 12,
						"rowSpan": 1,
						"column": 0,
						"row": 1,
						"layoutName": "AccountPageGeneralInfoBlock"
					},
					"bindTo": "UsrTeam"
				},
				"parentName": "AccountPageGeneralInfoBlock",
				"propertyName": "items",
				"index": 2
			},
			{
				"operation": "insert",
				"name": "UsrAmount3be3f18f-35c9-4656-b70d-1233ee4f9598",
				"values": {
					"layout": {
						"colSpan": 12,
						"rowSpan": 1,
						"column": 0,
						"row": 2,
						"layoutName": "AccountPageGeneralInfoBlock"
					},
					"bindTo": "UsrAmount"
				},
				"parentName": "AccountPageGeneralInfoBlock",
				"propertyName": "items",
				"index": 3
			},
			{
				"operation": "insert",
				"name": "UsrWord4a52daa1-827a-46d9-b926-f060eac88be3",
				"values": {
					"layout": {
						"colSpan": 12,
						"rowSpan": 1,
						"column": 12,
						"row": 2,
						"layoutName": "AccountPageGeneralInfoBlock"
					},
					"bindTo": "UsrWord"
				},
				"parentName": "AccountPageGeneralInfoBlock",
				"propertyName": "items",
				"index": 4
			},
			{
				"operation": "merge",
				"name": "EmployeesNumber",
				"values": {
					"layout": {
						"colSpan": 12,
						"rowSpan": 1,
						"column": 0,
						"row": 0
					}
				}
			},
			{
				"operation": "merge",
				"name": "Ownership",
				"values": {
					"layout": {
						"colSpan": 12,
						"rowSpan": 1,
						"column": 12,
						"row": 0
					}
				}
			},
			{
				"operation": "merge",
				"name": "AnnualRevenue",
				"values": {
					"layout": {
						"colSpan": 12,
						"rowSpan": 1,
						"column": 0,
						"row": 1
					}
				}
			},
			{
				"operation": "merge",
				"name": "ContactsAndStructureTabContainer",
				"values": {
					"order": 1
				}
			},
			{
				"operation": "merge",
				"name": "AccountPageServiceTab",
				"values": {
					"order": 2
				}
			},
			{
				"operation": "merge",
				"name": "RelationshipTabContainer",
				"values": {
					"order": 4
				}
			},
			{
				"operation": "merge",
				"name": "HistoryTabContainer",
				"values": {
					"order": 5
				}
			},
			{
				"operation": "merge",
				"name": "NotesTabContainer",
				"values": {
					"order": 6
				}
			},
			{
				"operation": "merge",
				"name": "ESNTab",
				"values": {
					"order": 7
				}
			}
		]/**SCHEMA_DIFF*/
	};
});

 where UsrAmount is an Integer column and UsrWord is a string column. Once the page is opened you should see this:

And you will also need to finalize the code so to include thousands in it and also add additional check for cases when "232" for example returns "two hundred thirty undefined".

 

Best regards,

Oscar

why it is showing undefined values? what are checks need to be applied?

 

Bhumika Bisht,

 

it shows undefined values since the check is performed by indexOf the string. And in case the string is 232 the indexOf 2 for this string returns 0, but there are two "2" symbols in the string so that's why the first one symbol is recognized as "two"m but the second one is not and the undefined value is set for it. Additional check that is used by arrays can cover this case as well, for example please examine the 55 case where division by 11 is performed in the

 

if (numValue%11===0){
            numValueDivisionBy11Check = numValue/11;
            stringConcatTyPart = String(tyArray[numValueDivisionBy11Check]);
            stringConcatUnitPart = String(unitsArray[numValueDivisionBy11Check]);
        }
        stringAfterFunctionValue = stringConcatTyPart + " " + stringConcatUnitPart;
        this.set("UsrWord",stringAfterFunctionValue);
        }

 

part of the function.

 

Best regards,

Oscar

Please check my code it is showing every value correct only value with 10000 it is showing ten thousand hundred and if I put 200000 two lakh thousand hundred. Otherwise, it is showing the correct output

convertNumberToWords:function(){

        var amount=this.get("UsrAmount");

    var words = new Array(0);

    words[0] = "";

    words[1] = "One";

    words[2] = "Two";

    words[3] = "Three";

    words[4] = "Four";

    words[5] = "Five";

    words[6] = "Six";

    words[7] = "Seven";

    words[8] = "Eight";

    words[9] = "Nine";

    words[10] = "Ten";

    words[11] = "Eleven";

    words[12] = "Twelve";

    words[13] = "Thirteen";

    words[14] = "Fourteen";

    words[15] = "Fifteen";

    words[16] = "Sixteen";

    words[17] = "Seventeen";

    words[18] = "Eighteen";

    words[19] = "Nineteen";

    words[20] = "Twenty";

    words[30] = "Thirty'";

    words[40] = "Forty";

    words[50] = "Fifty";

    words[60] = "Sixty";

    words[70] = "Seventy";

    words[80] = "Eighty";

    words[90] = "Ninety";

    amount = amount.toString();

    var atemp = amount.split(".");

    var number = atemp[0].split(",").join("");

    var nlength = number.length;

    var wordsstring = "";

    var j=0;

    var i=0;

    if (nlength <= 9) {

        var narray = new Array(0, 0, 0, 0, 0, 0, 0, 0, 0);

        var receivednarray = new Array(0);

        for (i = 0; i < nlength; i++) {

            receivednarray[i] = number.substr(i, 1);

        }

        var a=9-nlength;

        for (i = a, j = 0; i < 9; i++, j++) {

            narray[i] = receivednarray[j];

        }

        for ( i = 0, j = 1; i < 9; i++, j++) {

            if (i === 0 || i === 2 || i === 4 || i === 7) {

                if (narray[i] === 1) {

                    narray[j] = 10 + parseInt(narray[j]);

                    narray[i] = 0;

                }

            }

        }

         var value = 0;

          for ( i = 0; i < 9; i++) {

            if (i === 0 || i === 2 || i === 4 || i === 7) {

                value = narray[i] * 10;

            } else {

                value = narray[i];

            }

            if (value !== 0) {

                wordsstring =wordsstring+ words[value] + " ";

            }

           if ((i ===1 && value !== 0) || (i === 0 && value !== 0 && narray[i + 1] === 0)) {

                wordsstring = wordsstring+"Crores ";

            }

            if ((i ===3 && value !== 0) || (i === 2 && value !== 0 && narray[i + 1] === 0)) {

                wordsstring = wordsstring+"Lakhs ";

            }

            if ((i === 5 && value !== 0 )|| (i === 4 && value !== 0 && narray[i + 1] === 0)) {

                wordsstring =wordsstring+ "Thousand ";

            }

             if ((i === 6 && value !== 0) && (narray[i + 1] !== 0 && narray[i + 2] !== 0)) {

                wordsstring =wordsstring+ "Hundred and ";

            } else if(i === 6 && value !== 0) {

                wordsstring = wordsstring+"Hundred";

            }

        }

        //wordsstring = wordsstring.split("  ").join(" ");

       

    }

    this.set("UsrWord",wordsstring);

},

Bhumika Bisht,

 

Please add this additional check at the end of your function so to replace "Hundred and" part in case it is not required (in case the amount we path to the function can be divided by 100 without remainder):

 

else if (Number(amount)%100===0 &amp;&amp; Number(amount)!==100) {
                wordsstring = wordsstring.replace("Hundred and ","")
            } else if (Number(amount)===100) {
                wordsstring = wordsstring.replace("and ","")
            }

As a result you will receive the desired output:

Best regards,

Oscar

Hy Please also tell me how to apply check on the conditions: it should display seventy thousand five but instead, it is showing seventy thousand hundred and five.

Also please tell me this code works fine when I am trying in other sources why do I need to apply checks here?

Bhumika Bisht,

 

Thank you for your question, but I believe that this one is related to the JS code itself rather than the Creatio platform technical capabilities. Please debug the code to find out which checks should be applied so to make the output correct.

 

Best regards,

Oscar

Show all comments