Site Title

Homework

Arrays are like containers! They group multiple elements under one variable. You can see this by observing the code and the output. (run it)

%%html

  <h2>Output</h2>
  <div id="output"></div>

  <script>
    document.getElementById("output").innerText = ""; // Clear output
    (() => {

        let numbers = [1, 2, 3, 4];
        console.log(numbers);
        document.getElementById("output").innerText += numbers;


    })();
  </script>

Output

You can see that you only had to type one variable instead of multiple to get the group of numbers. Try it out yourself to get the concept

Part A of the homework (Graded for completion)

%%html

  <h2>Output 2</h2>
  <div id="outputpls"></div>

  <script>
    document.getElementById("outputpls").innerText = ""; // Clear output
    (() => {

        let weekendHobbies = ["Thinking", "Coding", "Badminton"];
        const sentence = "I like to: " + weekendHobbies;
        console.log(sentence);
        document.getElementById("outputpls").innerText += sentence;


    })();
  </script>

Output 2

Once you have ran the code, see how your list is generated without you having to type each individual word into the DOM. This concept applies to all other situations where you would want to reference your list.

Part B (Graded for completion)

%%html

  <h2>Output</h2>
  <div id="output3"></div>

  <script>
    document.getElementById("output3").innerText = ""; // Clear output
    (() => {

      
      let whatIDoAfterSchool = ["do homework quickly", "not failing school", "to enjoy life"];
      // Replace the blanks, that look like this _____, below! HINT: the line above
      const smallParagraph = "After school, I like to " + whatIDoAfterSchool[0] + ". This is because " + whatIDoAfterSchool[1] + " is really fun to me. I think you should try " + whatIDoAfterSchool[2] + "."; 
      console.log(smallParagraph);
      document.getElementById("output3").innerText += smallParagraph;


    })();
  </script>

Output

If you did that correctly, you’ll realize that you would’ve spent a LOT more time on that if you decided to type out your list every time it occured in those sentences instead of just using an array. When people use arrays, they have the goal of efficiency, and also organization. Arrays can help code from getting too lengthy, especially if the list if quite long and is referenced quite a lot.

Part C (Graded on whether it worked or not)

Try it out yourself!

%%html

  <h2>Output</h2>
  <div id="output4"></div>

  <script>
    (() => {
      let friends = ["Bob", "Tom", "Steve"];
      let output = document.getElementById("output4");
      output.innerHTML += `Welcome to the city of Anishiapolis, ${friends[0]}!<br>`;
      output.innerHTML += `Welcome to Pranayland, ${friends[1]}!<br>`;
      output.innerHTML += `You need to pay a fine of $300, ${friends[2]} :(`;
    })();
  </script>

Output

Submit here

https://docs.google.com/forms/d/e/1FAIpQLSfqHrQwvgNYvxxUxMD5mIfSgfZGkiDu6uPV0I_GMG8wqSQzpw/viewform?usp=header