The lastIndexOf(string)

The lastIndexOf method does pretty much what indexOf does, but it will start your search for the substring from the last. So, indexOf returns the position of the first occurrence of the substring and lastIndexOf returns the last occurrence of the substring:

const string = "this is an interesting book and                 this book is quite good as well.";console.log(string.lastIndexOf("this"))

The output from this is:

32

Although it might be a bit cumbersome to replace the endsWith string method with lastIndexOf, I still highly recommend you to give it a try and attempt to code it yourself. Once you're ready with your solution, check the following answer:

const string = "this is an interesting book and  this book is quite good as ...

Get Learn ECMAScript - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.