Question: Activity 1 : Helper functions to Load / Initialize and Store Json Lists In this activity we will write two functions that we will use

Activity 1: Helper functions to Load/Initialize and Store Json Lists
In this activity we will write two functions that we will use later in our server. Both functions are adapted from the example presented in the 'Node.js in Action' book (section 5.1.2). Follow along with the lecture videos to complete this activity.
STEP 1: Develop loadInitializeList to read a json list from a json file
1) Save the json array \([2,4,6,8]\) to the json file data. json in VScode.
2) Start a new .js file in VsCode and call it activity1.js.
3) Using asynchronous file APIs (Application Program Interface) read the json array from data.json
4) Convert the list to a JS array and print it to the screen. In case the json file does not exist, an empty list will be printed to the screen.
5) Run your script to ensure that it works correctly.
6) Update the script such that the code developed previously will be placed in a function called loadInitializeList. The function should take in the name of the json file to be read and a callback function. The callback function will return the list read from the file as a JS array if the file exists. Otherwise, the callback function will return an empty array
7) Call the loadInitializeList and pass to it the data.json and define the callback to print the list returned from loadInitializeList to the screen.
8) Run the script to ensure that it works correctly.
STEP2: Develop storeList to write a json list to a file
1) Define the function storeList that takes in as a parameter a file name and a JS array
2) Convert the JS array to a json array
3) Using the asynchronous file APIs write the json array to the file.
4) In case the write file operation returns an error, print the error message to the screen. Otherwise print a message indicating the successful write file operation
Testing activity1.js:
Using loadInitializeList() and storeList () functions, write a Nodejs script that will
1) Read a JSON list of numbers from the file data.json.
2) Double each number in the list.
3) Save the updated list back to the data.json file.
4) Account for scenarios where the file does not exist. In such cases, the script should utilize a list passed as an argument via the command line.
5) Run the script to ensure the it works correctly
6) Upload activity1.js to BB
Activity 1 : Helper functions to Load /

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!