Firstly, it looks like you don't test to see if testarray has any results. If it has no results, you won't be able to access the offsetTop property of the first result. I would add a test to see if you have results before trying to use them.
Secondly, it looks like you are trying to manipulate an element called chapter, but you don't define this anywhere, so the onscroll event handler won't work.
I hope this helps.