0

I am new at IIFE Functions and I'm having a hard time understanding how to implement it but I do understand what it is and what it's purpose is. The problem statement:

Your client wants to have a listing of zip codes from a zip code study (listed just once each) in order from least to greatest. He would like it to "just run" (self-invoking).

Here was my old code that was working and displaying the zipcodes appropriately. I will display my first initial code that did run, but is not considered self-invoking:

window.onload = displayUniqueZipcodes;

function assignment12_3() {
    // code goes in here. 
}

// Logic to display the zipcodes.
function displayUniqueZipcodes(){
    // Declare variables.
    var records, zip;
    var output = document.getElementById('outputDiv');
    var zipcodes=[];
    var outputString = "";   

    // Opens the records.
    records = openZipCodeStudyRecordSet();

    // Loops through records, pushes the unique records into the zipcodes array.
        while(records.readNextRecord()){
            zip = records.getSampleZipCode();
            if (!zipcodes.includes(zip)){
               zipcodes.push(zip); 
            }
        }

    // Sorts the zipcodes.
        zipcodes.sort();

    // outputs the zipcodes.
        for (var v in zipcodes){
            outputString += zipcodes[v] + "</br>";
        }
    output.innerHTML = outputString;   
}

& here is my code as of now that is not displaying the zipcodes anymore and my attempt of utilizing IIFE function:

function assignment12_3() {
    // Your code goes in here.   
    //Variables
    var records, zip;
    var output = document.getElementById("outputDiv");
    var zipcodes = [];
    var outputString = "";

    //Gets the records...
    records = openZipCodeStudyRecordSet();
    //This will loop through the records and put unique records
    //into an array
    while(records.readNextRecord()){
        zip = records.getSampleZipCode();
        if(!zipcodes.includes(zip)){
            zipcodes.push(zip);
        }
    }

    //Will sort the zipcodes
    zipcodes.sort();

    //outputs the zipcodes.
    for(var z in zipcodes){
        outputString += zipcodes[z] + "</br>";
    }

    outputDiv.innerHTML += outputString;

}();

Thank you.

  • Possible duplicate of [What is the (function() { } )() construct in JavaScript?](https://stackoverflow.com/questions/8228281/what-is-the-function-construct-in-javascript) – VLAZ Oct 06 '19 at 19:13
  • `(() => {console.log("hello world");})(); // IIFE hello world ` Just put your code there instead of the console log. – Alberti Buonarroti Oct 06 '19 at 19:15
  • 1
    You need to surround your function in `()` to turn it from a function *declaration* into an *expression*, you can then have an Immediately Invoked Function **Expression**. For the record, an alternative is to have `var assignment12_3 = function() {}()` - what you have now but you have to be assign it to a variable - then you're also dealing with a function expression. But people tend to avoid it, since it's hard to see the last `()` after the function body. – VLAZ Oct 06 '19 at 19:16
  • relevant about declarations/expressions: [var functionName = function() {} vs function functionName() {}](https://stackoverflow.com/questions/336859/var-functionname-function-vs-function-functionname) – VLAZ Oct 06 '19 at 19:17

0 Answers0