Lab 5: Midterm Review

Reminders

The midterm will be held during lecture on Monday. If you haven't started studying yet, do so today!

We will provide a detailed cheat sheet for you to use during the exam. You can print and bring any other notes or books you want (no computers/phones).

Good luck!

Exam Format

You should expect any of the following topics to be covered in the exam:

  • HTML (Writing and/or Validating)
  • CSS (Writing CSS and/or Selector Mysteries)
  • JavaScript Functions
  • JavaScript Events
  • JavaScript with AJAX
  • General Web Development Knowledge (short answer questions)

1. HTML (Validation)

An invalid HTML document is provided on the slide below. It generates many validation errors and warnings, however it is possible to make it validate by changing 5 lines in the HTML. Indicate what changes you could make to the document to make it pass validation.

1. HTML (Validation) Continued


                <html lang="en">
                <head>
                  <title> CSE 154 Hello, World!</title>
                </head>
                <body>
                
Hello
World!
<src="hello.jpg" alt="image of hello" img>
<p CSE 154 is the best class ever! #propaganda machine </p> <blockquote> The internet is a series of tubes. </body> </html>

HTML

Errors:

  1. ____________________________________
  2. ____________________________________
  3. ____________________________________
  4. ____________________________________
  5. ____________________________________

1. HTML (Validation) Solutions


                <html lang="en">
                <head>
                  <title> CSE 154 Hello, World!</title>
                </head>
                <body>
                
Hello
World!
<src="hello.jpg" alt="image of hello" img>
<p CSE 154 is the best class ever! #propaganda machine </p> <blockquote> The internet is a series of tubes. </body> </html>

HTML

Errors:

  1. All HTML documents need a DOCTYPE to inform the consumer what type of document this is
  2. Block elements are not allowed inside inline elements
  3. Malformed img tag - 'img' should be the first thing following the opening
    "<"
  4. Malformed <p> tag needs a closing bracket
  5. Blockquote elements need a closing tag

Problem 2: CSS (a.)

Open the source code of the provided HTML in a new tab (the document won't fit on this slide). For each of the following query selectors, indicate the id's of all of the HTML elements that would be found in the given HTML by the selector.

  • span
  • .banner-advertisement
  • .home-link
  • div a
  • div p, div > span

Problem 2: CSS (b.)

Using the provided HTML source code, implement index.css:

  1. Make the .content div float to the right and consume 75% of the width of the page.
  2. Make the text size of the advertisement link 32pt (gotta make that money)
  3. Make all <p> elements have no margin, border, or padding
  4. Make the background color of all menus black and have a 3px border that is solid and colored rebecaapurple
  5. Make the color of all links inside menus white

Problem 3: JavaScript

The following two problems (Problem 3 and Problem 4) use the provided HTML document. Save it and open it in a new tab through each part of Problems 3 and 4.

Problem 3: Part (a)

Implement sesame-street.js

(Big Bird Yellow): When the page loads, apply a style to the #cookie-header <h2> that sets the text color to hex value #f7f16d

Problem 3: Part (b)

(Count Chocula): Implement JavaScript in sesame-street.js such that when the page loads, you find all of the cookie list items in the cookie-jar, count them, and set the text of the #cookie-count <p> to be: < # of cookies>! There are <# cookies> in cookie(s) in the cookie jar!

(Replace '<# of cookies>' with the correct number).

Problem 3: Part (c)

(Cookie Monster Hungry): Implement JavaScript logic to remove the last cookie in Whitaker's cookie jar every 30 seconds. Make sure to update the #cookie-count when you remove a cookie: to accomplish this, you may assume that your solution from (b) is correct and functional.

Problem 3: JavaScript (a.) Solution


                window.onload = function() {
                  document.getElementById("cookie-header").color = "#F7F16D";
                };

                // helper functions
                function $(id) {
                  return document.getElementById(id);
                }

                function $$(query) {
                  return document.querySelectorAll(query);
                }
              

JS

Problem 3: JavaScript (b.) Solution


                window.onload = function() {
                  countCookies();
                };

                function countCookies() {
                  let cookies = $$("#cookie-jar li.cookie");
                  let count = $("cookie-count");
                  count.innerHTML = cookies.length  + "! There are " + 
                    cookies.length + " cookie(s) in the cookie jar!";
                }
              

JS

Problem 3: JavaScript (c.) Solution


                let timer = null;

                window.onload = function() {
                  timer = setInterval(eatTheCookies, 1000 * 30);
                };

                function eatTheCookies() {
                  let cookies = $$("#cookie-jar li.cookie");
                  if (cookies.length) {
                    let lastCookie = cookies[cookies.length - 1];
                    lastCookie.parent.removeChild(lastCookie);
                  } else {
                    // clear timer
                    timer = null;
                  }
                  // update cookie count (part b)
                  countCookies();
                }
              

JS

Problem 4: JavaScript with AJAX

Implement fetch-pie.js. fetch-pie.js should allow the user to click the 'Moar Pie!' button to fetch Whitaker's pies from a web service.

Make an AJAX GET request to a web service that lives at:
https://whitakers.pi.es/getPies.php

This web service returns a JSON object with this structure:

{ "pies" : 
  [
   {"type" : "pumpkin"}, 
   {"type" : "banana-cream"},
   {"type" : "gluten-free vegan sugar-free fat-free buttermilk 
              triple-chocolate fudge fantasy with extra rainbow sprinkles"}
  ]
}

Problem 4: JavaScript with AJAX (Continued)

Upon receiving the JSON, use JavaScript to insert one list item per pie into the #pie-cupboard <ul>. Each pie should have class 'pie'. The list item's text should contain only the type of pie (exactly as it appears in the 'type' property). Replace or remove any pies that were present in #pie-cupboard. For example, with the above JSON, you should insert the following list items into the #pie-cupboard <ul>:


              
  • pumpkin
  • banana-cream
  • gluten-free vegan sugar-free fat-free buttermilk triple-chocolate fudge fantasy with extra rainbow sprinkles
  • HTML

    If there is an error retrieving the pies, insert one list item into the #pie-cupboard <ul> with class 'error' and text that you choose that describes that an error occurred while fetching the pies. Replace or remove any pies that were present in #pie-cupboard.