// Description below the solution
const starWarsCelebration = (film, character) => {
const generalHttp = new XMLHttpRequest();
let finalFilm = [], finalCharacter = []
const globalHttp = (urlpath, data) => {
generalHttp.open("GET",
`https://challenges.hackajob.co/swapi/api/${urlpath}/?search=${data}`
, false);
generalHttp.send(null);
if(generalHttp.status === 200) return generalHttp
}
const loopRequest = (resUrl, arrayName, reqProp) => {
generalHttp.open("GET", resUrl, false)
generalHttp.send(null)
if(generalHttp.status === 200)
arrayName.push(JSON.parse(generalHttp.responseText)[reqProp])
}
const algoFunc = (pushArrayName, httpReq, propArray, propSubArray)
=> {
const jsonRes = JSON.parse(httpReq.responseText)["results"]
if(jsonRes.length < 1) pushArrayName.push("none")
else{
if(httpReq.status === 200){
const response = jsonRes[0][propArray]
if(response !== "undefined")
response.forEach(filmURL =>
loopRequest(filmURL, pushArrayName, propSubArray))
}
}
}
algoFunc(finalFilm, globalHttp("films", film), "characters", "name")
algoFunc(finalCharacter, globalHttp("people", character), "films",
"title")
return `${film}: ${finalFilm.sort().join(", ")}; ${character}:
${finalCharacter.sort().join(", ")}`
}
/*
We are in a Star Wars Celebration in 2015, a number of fans are
arguing about which characters appeared in which Star Wars films,
before the situation goes out of control we've decided to produce a
resource to help solving the issue.
Your job is, given a film name and a character name,
produce two lists, one with all the names of the characters who
appeared in the given film in alphabetical order, and another one
with all the names of the films where the given character appeared
in alphabetical order. Take into account that some self-proclaimed
fans might think Spock appeared in Return of the Jedi, in that case
please return "none" instead of the name of the films.
To do so, you will be using the Swapi API, which documentation can be
found here: https://challenges.hackajob.co/swapi/documentation,
which enpoint is: https://challenges.hackajob.co/swapi/api/.
It's really important to URL encode the film title and chatacter
name before calling the API.
Short Description
For a given character
(https://challenges.hackajob.co/swapi/api/people/?search= + character)
and
film (https://challenges.hackajob.co/swapi/api/films/?search= + film),
return a list with the film titles in which the character appears
in (sorted alphabetically) and another list with the character names
in the film (sorted alphabetically).Separate the titles and names
with commas and then separate both lists with a semicolon.
You can take a look over the examples presented below.
INPUT
string Film
string Character
OUTPUT
string Film: CharacterA, CharacterB, CharacterC;
Character: FilmA, FilmB, FilmC
EXAMPLE 1
Input
"A New Hope","Raymus Antilles"
Output
A New Hope: Beru Whitesun lars, Biggs Darklighter, C-3PO,
Chewbacca, Darth Vader, Greedo, Han Solo, Jabba Desilijic Tiure,
Jek Tono Porkins, Leia Organa, Luke Skywalker, Obi-Wan Kenobi,
Owen Lars, R2-D2, R5-D4, Raymus Antilles, Wedge Antilles,
Wilhuff Tarkin; Raymus Antilles: A New Hope, Revenge of the Sith
EXAMPLE 2
Input
"The Force Awakens", "Poggle the Lesser"
Output
The Force Awakens: Ackbar, BB8, Captain Phasma, Chewbacca,
Finn, Han Solo, Leia Organa, Luke Skywalker, Poe Dameron, R2-D2, Rey;
Poggle the Lesser: Attack of the Clones, Revenge of the Sith
EXAMPLE 3
Input
"The Force Awakens", "Walter White"
Output
The Force Awakens: Ackbar, BB8, Captain Phasma, Chewbacca, Finn,
Han Solo, Leia Organa, Luke Skywalker, Poe Dameron, R2-D2, Rey;
Walter White: none
DOCS: https://challenges.hackajob.co/swapi/documentation
*/
// With love @kouqhar