finally finished the javascript solution