Daily Coding Challenge - Offending Element

Tell us what’s happening:

How did they solve it and get the answer because it is confusing me

Your code so far

function findOffender(arr) {

  return arr;I didn't understand anything 
}

Your browser information:

User Agent is: Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/147.0.0.0 Mobile Safari/537.36

Challenge Information:

Daily Coding Challenge - Offending Element

https://www.freecodecamp.org/learn/daily-coding-challenge/2026-05-13

GitHub Link: https://github.com/freeCodeCamp/freeCodeCamp/blob/main/curriculum/challenges/english/blocks/daily-coding-challenge/69e2383af7832c8032603b92.md

step 1: do you understand what findOffender is supposed to do? Can you describe it to someone in your own words (describe it to a 5 year old kid who cannot read yet for example but knows numbers)?

step 2: do you know how you would solve it yourself if someone asked you to do the same thing that findOffender does but with a paper and pen instead? What algorithm would you use? Describe it.

step 3: write your algorithm into code

I don’t know, but these challenges seem very hard to me. I’ve worked on this challenge roughly three hours, and still haven’t gotten it right.

According to the instructions, all the numbers should be in ascending order. The first number that breaks the ascending order, is the number we should return, or I should say the index of it. Am I right?

If we take the first, they say findOffender([1, 6, 2, 3, 4, 5]) should return 1. Shouldn’t it return the index of 2? Assuming the array starts on index of zero, array[1] is the number 6, and array[2] holds the number 2.

The subarray [1, 6] (up to and including 6) is in perfect ascending order. If we add the next number, we get the array [1, 6, 2]. This last subarray is NOT in ascending order. Aren’t we supposed to catch the first number that breaks the ascending order? That number is array[2] (the number 2), not array[1], which is the number 6. What am I missing, if anything?

It returns 1 because 6 is the ‘offending’ number. If you remove 6, the numbers 1 and 2 to the left and right of the 6 are in ascending order as we want them to be.

Another example from the tests is:
findOffender([5, 18, 24, 33, 40, 55, 15, 68, 84, 91])

5 is less than 18, 18 is less than 24, 24 is less than 33, 33 is less than 40, 40 is less than 55
55 is not less than 15 but is less than 68, therefore 15 is ‘offensive’ and it’s index of 6 is returned.

Another example given:
findOffender([2, 4, 1, 6, 8])
2 is less than 4, 4 is not less than 1 but is less than 6, therefore 1 is offensive (it’s index of 2 is returned)

Step 1 of programming anything is always: do we understand the problem? If we don’t, how can we even attempt to solve it.

Hope this helps.

When you mentioned removing the element that breaks the ascending order, that’s what got me on track. If the element is removed from the array, and the rest of the array is in ascending order, then that was the offending element.

I have code now, that passes all the tests. For a long time (a few days) I only passed the first three tests, but not tests number 4 and 5. They are the hardest part. What I did was to make a helper function to check if the array - without the potentional offender - was sorted in ascending order.

Thanks for the help. I’m not happy with my code, but it’ll have to do. I can say I used the trial and error approach, maybe brute force, LOL!