js: factor out width calculation into a function