js assignment

Final refining done
shift this function on top (before using it) so that while reading anyone can know this function is defined before it is used