views:

76

answers:

8

I'm having some trouble finding unique entries in a record set. This is what I have so far.

function project5Part1() {

// Variable Declarations

var zipCode;
var outputDiv;
var records;
var i;
var j;
var null1;
var array = new Array(0);
var count;



// Get the HTML output table so we can add rows
outputDiv = document.getElementById('outputDiv');

// Open the Zip Code Study Records and make them 
// available to the script
records = openZipCodeStudyRecordSet();


//Logic
while (records.readNextRecord()) {

    zipCode = records.getSampleZipCode();
    j = zipCode;
    count++;


        if (j === array[0 - count]) {
            j = null1;

        }else {
            array.push(j);
        }

    }





// Output the entire data table
outputDiv.innerHTML = array;

}

I'm still pretty new to coding in general, and I've been banging my head on a wall for hours over this.

I think I'm pretty close to getting the program to do what I want, but I can't figure out how to check an array from one variable to another (In this case 0 through count) or if it is even possible.

Please help. :(

A: 

I don't know if I understood the question right. Are you looking for something like this?

for(i = 0; i < count; i++){
    // do something with array element i
    alert(array[i]);
}
RamboNo5
A: 

Just loop through the array from 0 to count:

for(var i=0; i<array.length; i++){
  if (j === array[i]) { ... }
}
Justin Ethier
A: 

Can you explain what you're trying to do a little more? First this code won't work:

array[0 - count]

You are trying to take a negative subscript of an array. Also, you should initialize count. Why are you subtracting count from 0?

BobbyShaftoe
A: 

Perhaps you can try this to prune duplicates from records:

var array = [];
while (records.readNextRecord()) {
    zipCode = records.getSampleZipCode();

    var duplicate = false;
    for (var i = 0; i < array.length; i++) {
        if (array[i] == zipCode) {
            duplicate = true;
            break; //do not check the rest of the array as zipCode is already a duplicate.

        }
    }
    if (!duplicate)
        array.push(zipCode);

}
Igor Zevaka
You'll want to change that. In your `if(array[i] == zipCode)` block, you'll never set `duplicate` to true because you break before you get to that line.
Jeff Rupert
Of course, thanks ;)
Igor Zevaka
A more efficient way of doing this is using a JavaScript object. In that case, you won't have to loop over the array each time to check to see if an element is in the set. In that case you can just see if it exists, and if not, add the zip code as the key with some dummy value.
Bialecki
A: 

It's sorting through a record set saved in another JS file. The record set is called ZipCodeStudyRecordSet(). There are about 900 total records in it, but only about 11 unique zip codes.

I need the program to sort through the zip codes, and add the unique zip codes to the array with array.push(j);.

The code above adds all the zip codes to the array, not just the unique ones.

The problem is in the if statement condition. what I want it to do is compare "j" to "array[0]" through "array[count]".

I'm not even sure if that's possible. If it isn't, how can I go about finding the unique zip codes in the record set?

JellyBean976
A: 

Then this should work within the while loop.

var newZipCode = true;
for(i = 0; i < array.length; i++){
if(array[i] == zipCode){
    newZipCode = false;
}
}
if(newZipCode){
   array.push(zipCode);
}
RamboNo5
A: 

Igor, your code works. <3!

JellyBean976
A: 

hmm. How would I be able to check to see if the code has gotten all of the unique zip codes? I'm pretty sure it has, but I have to verify it and state how I verified it. The list is far to large to compare the results of the program to.

JellyBean976