Skip to content
Advertisement

check next array element is same or not in javascript?

I want to check that next Element in my array is same

JavaScript

Here I have 7 Elements in my array in which same element is same as their before element but some are not.

What I want : If the next Element of my Element is not the same with the first one so automatically it print opps in place of next and then check for next. as I write in my code but my code is correct

Output I want = 12 oops 13 13 14 oops 15 15 16 oops

Output I’m geeting= 12 oops! 13 13 13 oops! 14 oops! 15 15 15 oops! 16 oops!

Anyone help me with this? I don’t know what to do.

Thank You

Advertisement

Answer

I’m not very sure that I understand logic and how to handle all edge cases, but here is variant which fits provided expected output:

JavaScript

note: what about multiple (more than 2) consecutive equal numbers?

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement