Checking two arrays - scope issue?

Hi all,

I am having some problems with loops in Javascript. could someone explain to me why the code below returns 0

const checkArr = (arr1,arr2) =>{

  let identical = 0;

  for(let i = arr1[0]; i < arr1.length; i++){

    for(let j = arr2[0]; i < arr2.length; i++){

      if (i === j) {

        identical ++;





return identical;


let test1 = ['A', 'B' , 'C' , 'D', 'E', 'F'];

let test2 = ['A', 'B' , 'C' , 'F', 'G', 'H'];


Thanks for your help!

arr1[0] and arr2[0] are strings, so you are setting i and j to be strings at he beginning.
i < arr1.length
may not behave the way you’d expect for strings.

Check the conditions for the for-loops.

possible code
  for(let i = 0; i < arr1.length; i++){  /* first index would be 0 */
    for(let j = 0; j < arr2.length; j++){  // i changed to j
      if (i === j) {

Thanks for the feedback.

having made those changes, it now returns 6. I believe this is because it is comparing the index of each array which would both be the same. I have altered the if statement as follows:

 if (arr1[i] === arr2[j])

and it now returns 4 because the arrays share 4 letters in common (“A”, “B”, “C” and “F”). However only A B and C share the same index so I add an and statement:

if (arr1[i] === arr2[j] && i === j) {
       identical ++;

It now returns the correct number(3). Although, this seems a bit of a janky solution to me. If one has a more elegant way to solve this I’d be interested to hear it.

If you only want to check whether you have the same thing at the same index, then you don’t need nested loops.

   for (let i = 0; i < Math.min(arr1.length, arr2.length); i++) {
      if (arr1[i] === arr2[i]) {

That works great, Thank you very much!
The reason I was trying to do that is because I am struck on the Challenge Project: Mysterious Organism (

part 5:
Your research team wants to be able to compare the DNA sequences of different P. aequor . You’ll have to add a new method ( .compareDNA() ) to the returned object of the factory function.

.compareDNA() has one parameter, another pAequor object.

The behavior of .compareDNA() is to compare the current pAequor ‘s .dna with the passed in pAequor ‘s .dna and compute how many bases are identical and in the same locations. .compareDNA() does not return anything, but prints a message that states the percentage of DNA the two objects have in common — use the .specimenNum to identify which pAequor objects are being compared.
The code from the project is below:

// Returns a random DNA base
const returnRandBase = () => {
  const dnaBases = ['A', 'T', 'C', 'G']
  return dnaBases[Math.floor(Math.random() * 4)] 

// Returns a random single strand of DNA containing 15 bases
const mockUpStrand = () => {
  const newStrand = []
  for (let i = 0; i < 15; i++) {
  return newStrand

function pAequorFactory(number, arr){
  return ({  
  specimenNum: number, 
  dna: arr,
  mutate: function() {
    let randomBase = returnRandBase()
    let selectedBase = Math.floor(Math.random()* this.dna.length)
    if(this.dna[selectedBase] !== randomBase){
     this.dna[selectedBase] = randomBase;
     return this.dna 
    else {
    return this.mutate()
//this is where I am stuck, I have rewrote it with the logic from the forum posts but it still returns 0
    compareDNA: function (pAequor) {
      let identicalBases = 0;
      for (let i = 0; i < Math.min(this.dna.length, pAequor.length); i++){ 
              if (this.dna[i] === pAequor.dna[i]) {
            identicalBases ++
         return identicalBases

let test1 = pAequorFactory(1,mockUpStrand())
let test2 = pAequorFactory(2,mockUpStrand())
let test3 = pAequorFactory(3,mockUpStrand())
let test4 = pAequorFactory(4,mockUpStrand())
let test5 = pAequorFactory(5,mockUpStrand())
let test6 = pAequorFactory(6,mockUpStrand())
let test7 = pAequorFactory(7,mockUpStrand())

let testArr = [test1,test2,test3,test4 ,test5,test6, test7 ]


Any help would be appreciated.

if you just want a ratio or proportion,
you could do something like:
let proportionOfIdentical = identicalBases / this.dna.length;
and if you want to return a percentage:
return proportionOfIdentical * 100;

But at the moment identicalBases is returned as 0, I don’t understand why. So I can’t yet move on to the ratio/percentage part.

(I don’t know if you would be able to run the code here: and see what I mean)

in the for-loop
you had
instead of

After that, it seemed to work as intended when I tested it with:

let dnaX = mockUpStrand();
let dnaXcopy = dna1.slice();

let testX = pAequorFactory(1,dnaX);
testX.mutate();    // changes testX.dna and dnaX slightly
let testY = pAequorFactory(2,dnaXcopy);


I changed that and it works. Thanks so much for your help !!