Help

Re: Javascript error - plese help

908 0
cancel
Showing results for 
Search instead for 
Did you mean: 
Andreas_Iacovid
6 - Interface Innovator
6 - Interface Innovator

I have this javascript code and it gets me an error when i run it at line 98 that my variable called “final” is not defined :ReferenceError: final is not defined.
my code is shown below. can someone let me know what is wrong? it seems my functions are not recognised

// Generate a random alphanumeric string and update a field

// Note that the generated string is random, but not necessarily unique

// The string generator function is cribbed from here:

// https://stackoverflow.com/questions/10726909/random-alpha-numeric-string-in-javascript

// The string generator function and Luhn mod-n checksum function

(function(){

var possible = '0123456789abcdefghijklmnopqrstuvwxyz';

 

function randomString(length) {

    var text = '';

    for( var i=0; i < length; i++) {

        text += possible.charAt(Math.floor(Math.random() * possible.length));

    }

    return text;   

}



function generateCheckCharacter(input) {



    var factor = 2;

    var sum = 0;

    var n = possible.length;



    // Starting from the right and working leftwards is easier since 

    // the initial "factor" will always be "2" 

    for (var i = input.length - 1; i >= 0; i--) {

            var codePoint = possible.indexOf(input.charAt(i));

            var addend = factor * codePoint;



            // Alternate the "factor" that each "codePoint" is multiplied by

            factor = (factor == 2) ? 1 : 2;



            // Sum the digits of the "addend" as expressed in base "n"

            addend = Math.floor(addend / n) + (addend % n);

            sum += addend;

    }



    // Calculate the number that must be added to the "sum" 

    // to make it divisible by "n"

    var remainder = sum % n;

    var checkCodePoint = (n - remainder) % n;



    return possible.charAt(checkCodePoint);

}



function isValid(input) {

    

    var factor = 1;

    var sum = 0;

    var n = possible.length;



    // Starting from the right, work leftwards

    // Now, the initial "factor" will always be "1" 

    // since the last character is the check character

    for (var i = input.length - 1; i >= 0; i--) {

            var codePoint = possible.indexOf(input.charAt(i));

            var addend = factor * codePoint;

         

            // Alternate the "factor" that each "codePoint" is multiplied by

            factor = (factor == 2) ? 1 : 2;



            // Sum the digits of the "addend" as expressed in base "n"

            addend = Math.floor(addend / n) + (addend % n);

            sum += addend;

    }



    var remainder = sum % n;

    

    return (remainder == 0);

}

// $(’#generate’).click(function() {

    var prefix = randomString(12);

    var check = generateCheckCharacter(prefix);

    var prefixa = randomString(3);

    let final = "CYP" + prefix + "-" + prefixa + "-" + check;

   // $('#result').val(prefix + "-" + check);

   // document.getElementById("insert").innerHTML = prefix+check;

})();

// Set the table

let randomTable = base.getTable(“Database 1 - Natural Persons”);

// Get the records

let randomQuery = await randomTable.selectRecordsAsync();

// Loop through the records

for (let record of randomQuery.records) {

// if the field is empty...

if(!record.getCellValue("Operator Registration")){

    // then generate a new random string for it..

    **let rString = final;**

    console.log(rString);

    // and update the field in the table

    await randomTable.updateRecordAsync(record, {

        "Operator Registration": rString

    })        

}

}

1 Reply 1
Mike_Pennisi
7 - App Architect
7 - App Architect

Hi @Andreas_Iacovides,

In that code sample, the binding final is declared inside a function, but it
is referenced outside of the function. It’s a bit hard to see because of the
formatting and all the other statements, so here’s a shorter version that
demonstrates the same problem:

(function() {
  var prefix = randomString(12);
  var check = generateCheckCharacter(prefix);
  var prefixa = randomString(3);
  let final = "CYP" + prefix + "-" + prefixa + "-" + check;
})();

if(!record.getCellValue("Operator Registration")){
  // then generate a new random string for it..
  let rString = final;
}

final can’t be accessed in the if statement because it was declared in the
function scope. One way around this would be to declare final outside of the
function. Then, you could assigned to it from inside the function, and you
could still access it from the if statement.

let final;
(function() {
  var prefix = randomString(12);
  var check = generateCheckCharacter(prefix);
  var prefixa = randomString(3);
  final = "CYP" + prefix + "-" + prefixa + "-" + check;
})();

if(!record.getCellValue("Operator Registration")){
  // then generate a new random string for it..
  let rString = final;
}