Provide solution and review my answer

Could you explain why my solution wasn’t 100% correct and what is the most feasible solution for this problem.
My code: