Introduction to JavaScript : Introduction to Request


#1

Using the Data muse api to get words that rhyme. I need help understanding the rest of the code after the boiler plate code for xmlHttpRequest is used. Why the need for the .map iterator .

const formatJson = (resJson) => {
  resJson = JSON.stringify(resJson);
  let counter = 0;
  return resJson.split('')
  .map(char => {
    switch (char) {
      case ',':
        return `,\n${' '.repeat(counter * 2)}`;
      case '{':
        counter += 1;
        return `{\n${' '.repeat(counter * 2)}`;
      case '}':
        counter -= 1;
        return `\n${' '.repeat(counter * 2)}}`;
      default:
        return char;
    }
  })
  .join('');
}

const renderResponse = (jsonResponse) => {
  const jsonSelection = Math.floor(Math.random() * 10);
  display.innerHTML = `<pre>${formatJson(jsonResponse[jsonSelection])}</pre>`;
}

const changeButton = () => {
  const newText = Math.floor(Math.random() * 7);
  jsonButton.innerHTML = `${collection[newText]}!`;
}

jsonButton.addEventListener('click', generateJson);

This is the link to the exercise https://www.codecademy.com/courses/introduction-to-javascript/lessons/requests-i/exercises/requests-intro-i?action=resume_content_item


#2

ohk so i now finally appreciate the code.