Skip to content
Advertisement

How to modularize code while creating HTML elements in JavaScript?

I am writing a piece of code to basically call in the top money earner and the top five money earners in a given data set. While writing the code, I realized that there were a couple of spots where I was rewriting the code, basically copying and pasting it. While that works, I wanted to throw the duplicate portion of the code and call it from a function. However, that is not working and I don’t exactly know why. Here is the code that is duplicated:

JavaScript

Here is the function I made to be called. However, when I call it, it’s not working as expected, where the information shows up on the webpage. I’m using VS Code and am running this on live server so when I save, the webpage automatically updates.

JavaScript

Advertisement

Answer

For one thing topFiveSalaries is going to be undefined in the function createHtmlElements you’ve created, you must pass it to the function

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement