reorganized on the basis of Idiomatic Javascript
forgetten variable name 'check' changed by 'state'