updated js with recursive function