-2

I'm sure I'm just missing something simple here, but I've hit a wall with this. I am attempting to run a loop through an array of names, and I want it to compare each name to the previous one to see if they are the same.

But what happens is as it goes through each name, it seems to be comparing it to itself, one character at a time, and going through a different character index each time.

Here is what I attempted:

var nameArray = ["Tony Stark", "Bruce Banner", "Bruce Banner", "Steve Rogers", "Steve Rogers"]
var nameComparisonArray = []
var element = ""

for (let index = 0; index < nameArray.length; index++) {
  const name = nameArray[index]
  const i = index

  if (name != name[i - 1]) {
    element = "" + name + " is different from " + (name[i - 1]) + ""

  } else if (name == name[i - 1]) {
    element = "" + name + " is the same as " + (name[i - 1]) + ""

  }
  nameComparisonArray.push(element)
}
console.log("nameComparisonArray", nameComparisonArray)

That returned this result:

[
  "Tony Stark is different from undefined",
  "Bruce Banner is different from B",
  "Bruce Banner is different from r",
  "Steve Rogers is different from e",
  "Steve Rogers is different from v"
]

What I want it to return is this:

[
  "Tony Stark is different from undefined",
  "Bruce Banner is different from Tony Stark",
  "Bruce Banner is the same as Bruce Banner",
  "Steve Rogers is different from Bruce Banner",
  "Steve Rogers is the same as Steve Rogers"
]

Anyone know how I can get my intended result? Any insight is appreciated!

Thanks!

Barmar
  • 741,623
  • 53
  • 500
  • 612
Jared
  • 1
  • 2
  • 3
    You'll want `nameArray[i - 1]` instead of `name[i - 1]`. – trincot Mar 21 '23 at 18:09
  • 3
    [How to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) and [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173) – VLAZ Mar 21 '23 at 18:12

1 Answers1

0

The problem is in this part:

if (name != name[i - 1]) {
    element = "" + name + " is different from " + (name[i - 1]) + ""

} else if (name == name[i - 1]) {
    element = "" + name + " is the same as " + (name[i - 1]) + ""

Remember that name is itself a string, just one of the names from the list that you got at the position index

const name = nameArray[index]

When you do name[i - 1], you are not getting another name from the list, you are getting the specific character at position i - 1 in the current name.

What you meant, instead, was to pick the previous one from the list, like this: nameArray[i - 1]

if (name != nameArray[i - 1]) {
    element = "" + name + " is different from " + (nameArray[i - 1]) + ""

} else if (name == nameArray[i - 1]) {
    element = "" + name + " is the same as " + (nameArray[i - 1]) + ""
Rodrigo Rodrigues
  • 7,545
  • 1
  • 24
  • 36
  • Amazing. As I suspected, I was definitely missing something simple. Not sure how I missed that, but thank you! That completely solved it, and I can now stop pulling my hair out lol – Jared Mar 21 '23 at 22:19