keypad.addEventListener(keypadEvent);

I'm confused how to fix code

Why do you think the improperly posted code needs fixing?

and also why that code which is keypad.addEventListener(keypadEvent); is declared in this scope?

What scope do you think it should be "declared" in? Code is not declared. Variables are.

If you foolishly insist on putting the { on the line with the function declaration or the if statement, atleastusesomespacesoitisrecognizable.