Skip to main content
Known Participant
November 17, 2024
Question

Script for Calculating LTV

  • November 17, 2024
  • 9 replies
  • 844 views

I'm trying to create a script to calculate the LTV based on several factors. If the TransactionType contains the word "Purchase" then the calculation is the InitialAdvance divided by the minimum of the PurchasePrice and AsIsValue. If the TransactionType contains the word "Refinance" then the calculation is the InitialAdvance divided by the AsIsValue. The ChatGPT script given hasn't worked property for some reason as listed below, hopefully someone can direct me the right way:

 

(function() {

    // Get the values from the form fields

    var initialAdvance = parseFloat(this.getField("InitialAdvance").value);

    var purchasePrice = parseFloat(this.getField("PurchasePrice").value);

    var asIsValue = parseFloat(this.getField("AsIsValue").value);

    var transactionType = this.getField("TransactionType").value;

 

    // Variable to store the LTV calculation result

    var ltv = 0;

 

    // Check the transaction type and perform calculations

    if (transactionType.includes("Purchase")) {

        // Calculate LTV using the minimum of PurchasePrice and AsIsValue

        var minValue = Math.min(purchasePrice, asIsValue);

        if (minValue !== 0) {

            ltv = initialAdvance / minValue;

        }

    } else if (transactionType.includes("Refinance")) {

        // Calculate LTV using AsIsValue

        if (asIsValue !== 0) {

            ltv = initialAdvance / asIsValue;

        }

    }

 

    // Set the calculated LTV value with appropriate formatting

    event.value = ltv.toFixed(2); // adjust the decimal places as needed

})();

This topic is closed to new replies. Start a new post to keep the conversation going.

9 replies

try67
Community Expert
Community Expert
November 17, 2024

The problem is this line, most likely:

if (transactionType.includes("Purchase")) {

Try this, instead:

if (transactionType.indexOf("Purchase")!=-1) {

 

Same with the other instance of includes.

Known Participant
November 18, 2024

Thanks for the reply; I tried that and unfortunately, that didn't fix the error.

try67
Community Expert
Community Expert
November 18, 2024

Are there any errors in the JS Console when you use the script? Can you share the file with us?