The CreatorCon Call for Content is officially open! Get started here.

onChange script error: TypeError: Cannot read properties of undefined (reading 'length') function () { [native code] }

Jk_
Tera Contributor

Hi All,

Getting below error, Please help.

12.92 - working fine

12.349 - working fine, rounded off to 12.35

12 - not working, getting below error.

find_real_file.png

find_real_file.png

1 ACCEPTED SOLUTION

Geoff_T
Mega Sage

Hello,

Your issue is with this line:

var finalcostStr1len = finalcost.split('.')[1].length;

Since 12 has no decimal point you cannot split on the '.' and get attempt to get the second index of an array that does not exist.

You might want to check that a decimal point exists before attempting a split on it.

So something like:

// check that decimal exists first
if(finalcost.indexOf('.') != -1){
    // do something..
}

 

Someone else I'm sure will have a nicer solution, but this at least should highlight the issue with your existing code 🙂

 

Geoff

View solution in original post

2 REPLIES 2

Valmik Patil1
Kilo Sage

Hello,

You add logic to check if value cost is float or integer

if it is float then only run your logic inside if block

Please refer below code

     n = 12 // n can be refer to as your Finalcost variable
     if(n === +n && n !== (n|0)){
   // your logic will run only when number entered is float
     gs.log("Float");
}

Please let me know if you need any other help

And correct me if I am wrongly understand your question

Thanks ,

Valmik

 

Geoff_T
Mega Sage

Hello,

Your issue is with this line:

var finalcostStr1len = finalcost.split('.')[1].length;

Since 12 has no decimal point you cannot split on the '.' and get attempt to get the second index of an array that does not exist.

You might want to check that a decimal point exists before attempting a split on it.

So something like:

// check that decimal exists first
if(finalcost.indexOf('.') != -1){
    // do something..
}

 

Someone else I'm sure will have a nicer solution, but this at least should highlight the issue with your existing code 🙂

 

Geoff