0

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;
			
		}
	}

2
  • Can you please demonstrate how you're creating this error? Commented Sep 30, 2014 at 20:50
  • You're going to run into all kinds of trouble with nested loops using the same variable name (which you really should be declaring with 'var' anyway). In JS, those are scoped to the nearest function, not the closure. Use different letters for inner loops. Commented Sep 30, 2014 at 20:51

2 Answers 2

4

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.

Sign up to request clarification or add additional context in comments.

2 Comments

You definitely want the iterator to increment AFTER the loop executes once in 99.99% of cases. Other than that, you're spot on. edit- the third statement executes after the loop anyway (@Sam) but there's no real reason to change it to ++i anyway.
@Antiga i will be incremented after the loop regardless of whether it's ++i or i++, so either one works. The increment statement isn't executed until the block inside the loop runs once.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.