-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path4-starwars_count.js
executable file
·30 lines (28 loc) · 1.11 KB
/
4-starwars_count.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#!/usr/bin/node
/**
* This Node.js script fetches data from the Star Wars API (SWAPI) and counts how many films a specific character appears in.
*
* It uses the 'process' module to access command line arguments and the 'request' module to make HTTP requests.
*
* The script expects the API URL as a command line argument (process.argv[2]).
*
* The 'request.get' function is used to make a GET request to the SWAPI. It takes two arguments:
* 1. The API URL
* 2. A callback function that is called when the HTTP request is complete.
*
* The callback function parses the response body as JSON, iterates over the films, and increments a counter each time it finds the character's URL in the film's characters array.
*
* Finally, it logs the count to the console.
*/
const request = require('request');
const apiURL = process.argv[2];
request.get(apiURL, (_, resp, body) => {
let characterRepeat = 0;
const results = JSON.parse(body).results;
results.forEach(film => {
if (film.characters.find((character) => character.endsWith('/18/'))) {
characterRepeat += 1;
}
});
console.log(characterRepeat);
});