Skip to content
Advertisement

how to compare the two names in array list

I am trying to compare the two names in the baby array list if it is true it will return “Two babies in the array are the same” if false will return “No same baby in the array”. But I just can’t get it to work for the findTwoSameBabies method, it is supposed to compare all the names in the array and return the result.

JavaScript

}

Advertisement

Answer

There are many problems with your code.

  • You have a most outer loop that iterates over the list, this is fine
  • You have a loop inside this loop that iterates over with an index
  • And an another, similar loop that goes ahead with one element from this element
  • But then you compare a name of the baby you get first with the size of the array. This is a number, and if your baby is not called as like 42 then you always get false in this case.

I honestly do not know what’s your real job to do here. I assume this is a homework and you have to prove your loop programming skills. Given this assumption, I recommend you the following solution:

JavaScript
  • We iterate over the list elements in the outer loop
  • And iterate over the same list in the inner loop
  • We compare every baby with every other babies to check duplicates
  • And break both loops if we found same names
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement