corrected js code, functioning properly