Rewriting the JS code, using object and arrays.