better but longer JavaScript solution