This application is designed for guessing zip codes, using FireBase and Angular technologies. Whenever the user inputs a digit, a query is sent to a FireBase database which returns an array of zip codes containing that specific digit in the corresponding position (lines 3-48). Subsequently, the newGuess() function is invoked. The returned arrays are stored in a compareArray for each inputted number (lines 50 - 66). The length of compareArray determines the next steps, where I encountered difficulties while attempting to iterate through the nested arrays. Despite some success, there were instances where the arrays didn't get printed. Could this be due to delayed population of the arrays? Perhaps my usage of $scope is incorrect.
It's worth mentioning that both compareArray and resultsArray were experimented with inside and outside the $scope object, yielding similar outcomes. Although a regular for loop was attempted for iteration, angular.forEach seemed more reliable.
The ultimate objective is to identify and extract nested arrays within compareArray, iterate through them, and compare the contained zip codes to find matches across all arrays. These matched zip codes would then be added to a resultsArray, from which one random code would be selected as the computer's "guess" for the user's location.
I acknowledge certain parts of the code aren't as efficient as they could be. My primary focus was on functionality first, with plans to refactor later. Any guidance on enhancing code efficiency would be highly valued.
For those interested in exploring further, the GitHub link can be accessed here: https://github.com/nhwilcox/zip-code_angular
zipCodeApp.controller('ZipCodesCtrl', function ZipCodesCtrl($scope, $firebaseArray) {
var ref = new Firebase('https://zip-it.firebaseio.com/zips');
$scope.detectChangeDigit1 = function() {
var query = ref.orderByChild("digit1").equalTo($scope.zipCode.firstDigit.toString());
$scope.digit1Array = $firebaseArray(query);
$scope.digit1Array.$loaded().then(function() {
$scope.digit1Array.sort();
});
newGuess();
};
// Additional detectChangeDigit functions for digits 2 to 5
var newGuess = function() {
var compareArray = []
// Logic for creating and comparing arrays
$scope.resultsArray = resultsArray;
}
});