Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

Star Wars Celebration

// 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
Comment

PREVIOUS NEXT
Code Example
Javascript :: This is the JSON 
Javascript :: js set height of element 
Javascript :: I want to filter the below json data by start date and end date, it should return the data between start date and end date, 
Javascript :: how to use session with cookie js nodejs 
Javascript :: discord.js Function to replace certain characters with a pattern 
Javascript :: html document from string javascript 
Javascript :: nodejs express parse query params boolean 
Javascript :: backbone.js validation 
Javascript :: Return object in parenthesis to avoid it being considered a wrapping function body 
Javascript :: JSON Use Example 
Javascript :: react axios POST with super constructor parent class 
Javascript :: new date is not working in react js 
Javascript :: move an object in array by latest clicked 
Javascript :: map function usage in frontend 
Javascript :: telerik grid destroy table 
Javascript :: eva icons js 
Javascript :: concat vs spread 
Javascript :: how to confirm if angular js in installed 
Javascript :: Nested Components 
Javascript :: for loop increment by more than one 
Javascript :: Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.6.0:compile 
Javascript :: javascript program to find largest of 2 numbers 
Javascript :: decode jwt token online 
Javascript :: random jwt secret key generator 
Javascript :: TypeError: Invalid schema configuration: `True` is not a valid type at path `id.required`. See https://bit.ly/mongoose-schematypes for a list of valid schema types.] 
Javascript :: how to convert numbers to roman numerals in javascript 
Javascript :: draw image inside canvas width 100% 
Javascript :: MAP METHOD. IMPORTANT 
Javascript :: Passing JSON to Javascript in Laravel – but JS is converting the JSON to HTML Entities 
Javascript :: angularjs Add aria-label to table header in datatable 
ADD CONTENT
Topic
Content
Source link
Name
6+8 =