Reputation: 11
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.
Upvotes: 0
Views: 59