javascript - Tell if string is pixels or percent then divide by two -
so have function allow user define custom width either pixels or percents.
the problem need half of number provide. here example of trying do:
user enters----------expected result
50% 25% 300px 150px any appreciated!
this works, although should validate input first.
parseint(str) / 2 + str.match(/(%|px)$/)[0] without validation, throws error if input doesn't end in % or px (can't null[0]). if instead want have missing or unrecognized units treated % default, this:
parseint(str) / 2 + (str.match(/(%|px)$/)||["%"])[0]
Comments
Post a Comment