What is the best approach to solve the following problem in Javascript? I have attempted to solve it but i am running into too much trouble, it is not even worth it to post