CaptainLazy
CaptainLazy

Reputation: 3

JavaScript For Loop Keeps Looping Infinity

I've written the functions below as part of a much larger application for processing FASTA formatted files via a web interface. For some reason it decided to run into infinity when call upon my baseCounts() function from within makePretty(). It might be worth noting that both functions are encapsulated by the same parent function.

The function baseCounts() returns valid data in the form of a 100+ long array, console.log confirms that it is not to blame so the problem has to be with makePretty().

Any help is welcome.

function baseCount(records){
		// Count instances of Bases in array
		var basecounts = Array();
		for (i=0; i < records.length; i++){
			var record = records[i];
			console.log(record);
			var count = [record.match(/A/g), record.match(/T/g), record.match(/C/g), record.match(/G/g)];
			var basecount = Array();
			for (i=0; i < count.length; i++){
				basecount.push(count[i].length);				
			}
			// return array of occurance
			basecounts.push(basecount);
		}
	}
	
	function makePretty(fasta){
		// Make FASTA more human friendly
		
		var data = Array();
		var basecounts = Array();
		var bases = Array();
		console.log(fasta.length);
		
		// Generate base array 
		for (i=1; i < fasta.length; i++){
			bases.push(fasta[i][2])
		}
		basecounts = baseCount(bases); // RUNS INTO INFINITY
		
		
		for (i=0; i < fasta.length; i++){
				
			var record = Array();
			record.push(i); // Add protein number
			record.push(fasta[i][0]); // Add NC_#
			record.push(fasta[i][1]); // Add base index
			_record = fasta[i][2];
			var l_record = _fasta.length; // Protein length
			//var basecount = baseCount(_record);
			var cg_content;
			
		}
	}

Upvotes: 0

Views: 1207

Answers (2)

Organiccat
Organiccat

Reputation: 5651

You're reseting your variable counter in your inner loop (i).

To avoid this, and future problems like it as well as hoisting issues, I would suggest using the newer functions such as forEach or map. You can also clean up your code this way:

function baseCountFunc(records){
  // Count instances of Bases in array
  var basecount = [];
  records.forEach(function(record) {
    var count = [record.match(/A/g), record.match(/T/g), record.match(/C/g), record.match(/G/g)];
    count.forEach(function(countElement) {
      basecount.push(countElement.length);
    });
    basecounts.push(basecount);
  });
}

Also, I noticed you named your function the same name as your variables, you should avoid that as well.

Upvotes: 0

Jerry101
Jerry101

Reputation: 13367

Your nested loops are using the same variable i, and clobbering each other's state.

        for (i=0; i < records.length; i++){
            ...
            for (i=0; i < count.length; i++){
                ...
            }

Use distinct variables, say i and j or better yet pick meaningful names.

Also you should declare the variables (var i) to ensure they're local to the function.

Finally, use ++i, not i++. The former means "increment i" while the latter means "i, and oh by the way increment it". They both increment i, but the latter one returns the old value, which is a special language feature to use in special cases.

Upvotes: 4

Related Questions