Using a JavaScript recursive function