Lessons 126 to 127 – Complete 2020 Web Development Bootcamp

Lesson 126 – Collections: Working With Javascript Arrays

An array is a collection of items that are related and that can be stored together in the same container using the same variable.

The syntax for an array looks like this:

var eggs = ["Egg 1", "Egg 2", "Egg 3", "Egg 4", "Egg 5"]

You can make a new variable and assign one of the items in that array to the new variable like this:

var myEgg = eggs[1];

The variable myEgg now equals “Egg 2” from the array “eggs”.

You can use the includes function with an array to find out if an array contains a particular item. For example:

var eggs = ["Egg 1", "Egg 2", "Egg 3", "Egg 4", "Egg 5"]

eggs.includes("Egg 4");

This will return “true” because the array named “eggs” contains “Egg 4”. If instead we had used the following code, it would return “false” because the array “eggs” does not include “Egg 6”.

var eggs = ["Egg 1", "Egg 2", "Egg 3", "Egg 4", "Egg 5"]

eggs.includes("Eggs 6");

The challenge in this lesson is to write code that will:

  • prompt the user for his name;
  • check the name given by the user against the list of names in the guest list array;
  • if the name is on the guest list array, give the user a welcome message;
  • if the name is not on the guest list, advise the user that his name is not on the guest list.

This is my solution:

var guestList = ["Angela", "Jack", "Pam", "James", "Lara", "Jason"];

var guestName = prompt("What is your name?");

if (guestList.includes(guestName) ) {
    alert("Welcome! You are on the guest list!")
}

else {
    alert("Sorry, you are not on the guest list.")
}

Lesson 127 – Adding Elements and Intermediate Array Techniques

The push function can be used to push new items into an array.

var output = [];

output.push(1);

The push function always pushes the item in the parenthesis to the end of the array.

The pop functions is used to pop items off the end of an array. It looks like this:

output.pop;

The challenge in this lesson was to create a “FizzBuzz” program which pushes the numbers 1 to 100 into an array but if the number is divisible by 3 it pushes the word “Fizz” into the array instead of the number. If the number is divisible by 5 it pushes the word “Buzz” into the array instead of the number. If the number is divisible by both 3 and 5 it pushes the word “FizzBuzz” into the array instead of the number.

I wasn’t able to work this one out completely on my own. I had to watch the solution on the lesson video before I was able to complete it.

This is my solution:

var output = [];
var count = 1;

function fizzBuzz() {
    
    if (count % 3 === 0 && count % 5 === 0) {
        output.push("FizzBuzz")
    }
    
    else if (count % 3 === 0) {
        output.push("Fizz")
    }

    else if (count % 5 === 0) {
        output.push("Buzz")
    }

    else {
        output.push(count);
    }

    count = count + 1;

    console.log(output);

}

fizzBuzz();
fizzBuzz();
fizzBuzz();
fizzBuzz();
fizzBuzz();
fizzBuzz();
fizzBuzz();
fizzBuzz();
fizzBuzz();
fizzBuzz();
fizzBuzz();
fizzBuzz();
fizzBuzz();
fizzBuzz();
fizzBuzz();