Advertisement
makispaiktis

Codecademy - 3rd Exercise - Forms (MAIN JS)

Aug 26th, 2019 (edited)
346
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Grab values from the submitted form in the URL
  2. const words = new URLSearchParams(window.location.search);
  3.  
  4. // Cleans up and capitalizes the names of the animals
  5. function cleanAndCap (str){
  6.   if(!str) return null
  7.   let temp = str.trim()
  8.   return temp[0].toUpperCase() + temp.substring(1)
  9. }
  10.  
  11. // Assigning the variables with values used in the story
  12. const firstAnimal= cleanAndCap(words.get('animal-1'));
  13. const secondAnimal = cleanAndCap(words.get('animal-2'));
  14.  
  15. const answer = cleanAndCap(words.get('answer'));
  16. const conjunction = answer === 'Yes' ? 'and' : 'but';
  17.  
  18. const speed = words.get('speed');
  19. const adj1 = words.get('adj-1');
  20.  
  21. const thirdAnimal = cleanAndCap(words.get('animal-3'));
  22. const quote = words.get('quote');
  23.  
  24. const verb1 = words.get('verb-1');
  25. const num1 = words.get('num-1');
  26. const message = words.get('message');
  27.  
  28. // The string containing HTML and text which will populate the story.html page
  29. const story = `<p>A <span class="word" title="id: animal-1">${firstAnimal}</span> was making fun of the <span class="word" title="id: animal-2">${secondAnimal}</span> one day for being so slow.</p>
  30.  
  31. <p>"Do you ever get anywhere?" he asked with a mocking laugh.</p>
  32.  
  33. <p>"<span class="word" title="id: answer">${answer}</span>," replied the <span class="word" title="id: animal-2">${secondAnimal}</span>, "${conjunction} I get there <span class="word" title="id: speed">${speed}</span> than you think. I'll run you a race and prove it."</p>
  34.  
  35. <p>The <span class="word" title="id: animal-1">${firstAnimal}</span> was much <span class="word" title="id: adj-1">${adj1}</span> at the idea of running a race with the <span class="word" title="id: animal-2">${secondAnimal}</span>, but for the fun of the thing he agreed. So the <span class="word" title="id: animal-3">${thirdAnimal}</span>, who had consented to act as judge, marked the distance yelled, "<span class="word" title="id: quote">${quote}</span>".</p>
  36.  
  37. <p>The <span class="word" title="id: animal-1">${firstAnimal}</span> was soon far out of sight, and to make the <span class="word" title="id: animal-2">${secondAnimal}</span> feel very deeply how ridiculous it was for him to try a race with a <span class="word" title="id: animal-1">${firstAnimal}</span>, he went off the course to practice <span class="word" title="id: verb-1">${verb1}</span> for <span class="word" title="id: num-1">${num1}</span> hours until the <span class="word" title="id: animal-2">${secondAnimal}</span> should catch up.</p>
  38.  
  39. <p>The <span class="word" title="id: animal-2">${secondAnimal}</span> meanwhile kept going slowly but steadily, and, after a time, passed the place where the <span class="word" title="animal-1">${firstAnimal}</span> was <span class="word" title="id: verb-1">${verb1}</span>. The <span class="word" title="id: animal-1">${firstAnimal}</span> was so caught up in <span class="word" title="id: verb-1">${verb1}</span>; and when at last he did stop, the <span class="word" title="id: animal-2">${secondAnimal}</span> was near the goal. The <span class="word" title="id: animal-1">${firstAnimal}</span> now ran his swiftest, but he could not overtake the <span class="word" title="id: animal-2">${secondAnimal}</span> in time.</p>`;
  40.  
  41. // Grabbing the title element
  42. const title = document.getElementById('title');
  43. // Populating the title element with text
  44. title.innerHTML = `The <span class="word" title="id: animal-1">${firstAnimal}</span> And The  <span class="word" title="id: animal-2">${secondAnimal}</span>`;
  45.  
  46. // Grabbing the story element
  47. const storyEl = document.getElementById('story');
  48. // Populating the story element with the value of the story variable
  49. storyEl.innerHTML = story;
  50.  
  51. // Grabbing the moral-message element
  52. const moralMessage = document.getElementById('moral-message');
  53. // Populating the moral-message element with text
  54. moralMessage.innerHTML = `<span class="italics" title="id: message">"${message}"</span>`;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement