Javascript Requests

I am on the fifth page of the Requests and I would like to know what the helperFunction.js file does ? And why is it there, also what does the renderResponse function do inthe main.js file ?

https://www.codecademy.com/courses/introduction-to-javascript/lessons/requests-i/exercises/xhr-get-requests-iii?action=resume_content_item

helperFunctions.js is a companion file that gets imported along with main.js at load time. They both occupy the same namespace so could as easily be all one big file. Breaking up the code this way prevents changes being made to the helper functions, and also allows other files to make use of it.

It checks the response length and if there is content in the response, it parses it to an array and outputs it to the browser window.

Ok so how does main.js have access to helperFunctions.js if there no import and export statements present ?

They are loaded into the namespace by the HTML script tags.

1 Like

Thank you for your response its very much appreciated, but I have one more question so how are the results being added to the HTML ? I looked through the code and couldn’t see how it was getting there.

1 Like

The results are inserted in the DOM as element nodes populated with the data.

So it just knows to go to that specific element ? I am a little confused still and am trying to grasp this concept as best as I can.

responseField.innerHTML = `<p>You might be interested in:</p><ol>${wordList}</ol>`;

responseField is a cached element node that we can access at any time with expressions such as above.

const responseField = document.querySelector('#responseField');

We declared it in main.js but since both scripts occupy the same namespace, both can see what the other’s objects are.

1 Like

Ok cool thanks alot for the reply I think I understand how the JSON file is being added in. I’m guessing its available in a variable called JSON ? Since we have made a request and the two files share the same namespace. Is that correct ?

JSON is a special JavaScript class that has methods for creating and parsing JSON files. The data is in the response. Note that there is only one namespace for all of JavaScript. Data may exist globally or be channeled through parameters and returns.