Skip to content

Instantly share code, notes, and snippets.

@OdearOgy
Created September 24, 2018 07:52
Show Gist options
  • Select an option

  • Save OdearOgy/c638c20f06f8b7a7149c4ef81e615966 to your computer and use it in GitHub Desktop.

Select an option

Save OdearOgy/c638c20f06f8b7a7149c4ef81e615966 to your computer and use it in GitHub Desktop.
// Formats response to look presentable on webpage
const renderResponse = (res) => {
// Handles if res is falsey
if(!res){
console.log(res.status);
}
// In case res comes back as a blank array
if(!res.length){
responseField.innerHTML = "<p>Try again!</p><p>There were no suggestions found!</p>";
return;
}
// Creates an empty array to contain the HTML strings
let wordList = [];
// Loops through the response and caps off at 10
for(let i = 0; i < Math.min(res.length, 10); i++){
// creating a list of words
wordList.push(`<li>${res[i].word}</li>`);
}
// Joins the array of HTML strings into one string
wordList = wordList.join("");
// Manipulates responseField to render the modified response
responseField.innerHTML = `<p>You might be interested in:</p><ol>${wordList}</ol>`;
return
}
// Renders response before it is modified
const renderRawResponse = (res) => {
// Takes the first 10 words from res
let trimmedResponse = res.slice(0, 10);
// Manipulates responseField to render the unformatted response
responseField.innerHTML = `<text>${JSON.stringify(trimmedResponse)}</text>`;
}
// Renders the JSON that was returned when the Promise from fetch resolves.
const renderJsonResponse = (res) => {
// Creates an empty object to store the JSON in key-value pairs
let rawJson = {};
for(let key in response){
rawJson[key] = response[key];
}
// Converts JSON into a string and adding line breaks to make it easier to read
rawJson = JSON.stringify(rawJson).replace(/,/g, ", \n");
// Manipulates responseField to show the returned JSON.
responseField.innerHTML = `<pre>${rawJson}</pre>`;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment