Fixed JS error with a conditional statement