How is this an infinite loop?

This seems to create an infinite loop but i don’t understand why. When i run it in Visual studio it seems to output the consol.log for every result and keeps doing so forever but as I understand it it should just finish the loops and then log the result. What is it I’m doing wrong?
Here is the function that doesn’t work:

const mostAlike = arr => {
    let matchOne;
    let matchTwo;
    let matchPercent = 0;
    for (i=0; i<arr.length; i++) {
        for(j=0; j<arr.length; j++) {
            if(arr[i] != arr[j]) {
                let tempMatchPercent = arr[i].compareDNA(arr[j]);
                if(tempMatchPercent > matchPercent) {
                    matchPercent = tempMatchPercent;
                    matchOne = arr[i];
                    matchTwo = arr[j];
                }
            }
        }
    }
    console.log(`${matchOne} and ${matchTwo} are the two most alike samples at ${matchPercent}%`)
}

Here is the full program I’m trying to make:
WARNING! DO NOT CLICK RUN! I only include it only so that you can test the solution if you think you know the problem

//test aBase = ['A', 'T', 'C', 'G', 'A', 'T', 'C', 'G', 'A', 'T', 'C', 'G', 'A', 'T', 'C'] // Returns a random DNA base const returnRandBase = () => { const dnaBases = ['A', 'T', 'C', 'G'] return dnaBases[Math.floor(Math.random() * 4)] } // Returns a random single stand of DNA containing 15 bases const mockUpStrand = () => { const newStrand = [] for (let i = 0; i < 15; i++) { newStrand.push(returnRandBase()) } return newStrand } const pAequorFactory = (specimenNum, dna) => { return { specimenNum, dna, mutate() { const randIndex = Math.floor(Math.random()*dna.length); let base = dna[randIndex]; let randBase = returnRandBase(); while (base === randBase) { randBase = returnRandBase(); } dna = dna.splice(randIndex, 1, randBase); }, compareDNA(pAequorObj) { let otherDNA = pAequorObj.dna; let count = 0; let percent = 0; for (i=0; i<otherDNA.length; i++) { if(otherDNA[i] === dna[i]){ count++; } } percent = (count/dna.length) * 100; console.log(`Specimen #${this.specimenNum} and Specimen #${pAequorObj.specimenNum} have ${percent}% of their DNA in common`) }, willLikelySurvive() { let count = 0; let percent = 0; for (i=0; i<this.dna.length; i++) { if (this.dna[i] === 'C' || this.dna[i] === 'G') { count++; } } percent = (count/this.dna.length) * 100; if (percent >= 60) { return true; } else { return false; } } } } //creates several organisms, chose how many through num const createSamples = num => { let sampleArr = []; for (let i=1; i<=num; i++) { let tempSample = pAequorFactory('model' + i, mockUpStrand()) if (tempSample.willLikelySurvive() === true) { sampleArr.push(tempSample) } else { i--; } } return sampleArr; } const thirtyArr = createSamples(30); const mostAlike = arr => { let matchOne; let matchTwo; let matchPercent = 0; for (i=0; i<arr.length; i++) { for(j=0; j<arr.length; j++) { if(arr[i] != arr[j]) { let tempMatchPercent = arr[i].compareDNA(arr[j]); if(tempMatchPercent > matchPercent) { matchPercent = tempMatchPercent; matchOne = arr[i]; matchTwo = arr[j]; } } } } console.log(`${matchOne} and ${matchTwo} are the two most alike samples at ${matchPercent}%`) } mostAlike(thirtyArr); //console.log(thirtyArr); //thirtyArr[29].mutate(); //console.log(thirtyArr[29]); /* //const model1 = pAequorFactory(1, aBase); //const model2 = pAequorFactory(2, aBase); const model1 = pAequorFactory(1, mockUpStrand()); const model2 = pAequorFactory(2, mockUpStrand()); //model1.mutate(); model1.compareDNA(model2); //model2.mutate(); //model1.compareDNA(model2); console.log('model 1 ' + model1.dna); console.log('model 2 ' + model2.dna); */

Hi,
I haven’t checked your code in detail. But I ran it in fiddle.js. It is not infinite. It just takes very long.
You do not only have a nested loop in the snippet you posted separately, but here:

you run a second nested loop:

for (let i=0; i<otherDNA.length; i++) {
                if(otherDNA[i] === dna[i]){
                    count++;
                }
            }

What I changed before I ran it:

I added the keyword let in each loop to initialize the variables i and j. Maybe that’s what’s letting the codebyte crash. But I followed your warning and didn’t click run :wink:

Wow, yeah. That kept it from running forever. And now that it actually stopped i realized where all the logs were coming from (the compareDNA function).

it still doesn’t work as intended but now I at least have a chance to figure out why.

1 Like