I’ve Cracked the Code, Please Call Tech Support- Episode 11, Wrote One (More Code)

Moving right along! Things seem a bit more cheery around the city today, which is rad. I know that 2020 has been weighing on all of us, some much more harshly than others. Seeing smiles in the eyes of everyone is a very nice change of pace, even for a moment.

  1. Describe one thing you’re learning in class today.

Some of the most convenient things that we’re learning in class are methods, and we just found out about two: .find(), and .findIndex().

.find allows us to locate the first element in an array that passes a test. So, if we have an array of numbers, and we want to find the first number that’s, say, below 5, we can use .find to return that number. We can also use .findIndex to tell us where in the array that number occurred.

2. Can you describe the main difference between a forEach loop and a .map() loop and why you would pick one versus the other?

Both of these loops are helpful! A .forEach loop goes through an array and executes a function once for each element. If you want to add two to each number in your array, and return the result, a .forEach loop works for that.

A .map loop also executes that function, but returns a new array with your new values. This can be useful in certain circumstances, as it doesn’t affect your original array.

3. Describe event bubbling.

Event bubbling occurs when a child element is effected by something, and that “bubbles” up through the parent elements.

As described here, let’s say you’ve got a button within an element. When it’s clicked, that effect ripples through the parent elements, meaning that the button, it’s section, and the body of the page were all clicked, effectively.

4. What is the definition of a higher-order function?

As per Eloquent JavaScript, a higher-order function is a function that operates on other functions, either by taking them as arguments or by returning them. It goes on to say that these allow us to abstract over actions, not just values.

5. ES6 Template Literals offer a lot of flexibility in generating strings. Can you give an example?

In coding, you often run into the situation of console.log-ing a value, but you want to make it a sentence that’s more readable by humans. So, you end up having to make something that looks like this:

console.log(“This value is “ + value + “, which is helpful.”)

Not illegible, but you can see how this would quickly spiral if you added more values to the mix. With template literals, we can shorten this down a bit, into something that looks like this:

console.log(“This value is `${value}`, which is helpful.”)

6. What is an associative array in JavaScript?

This type of array is basically on object, which we’ve spoken about previously. The only difference here is that the indexes are replaced with keys, that are defined by the user.

7. Why should you never use new Array in JavaScript?

A lot of this comes down to cleaning up code. Using new Array() will create an array, sure, but it gets confusing.

Want to create a new array, and have the number 10 as your one entry?

let numberArray = new Array (10)

The code above is intuitive…but it does not work.

Rather than having one entry that is 10, it creates 10 blank entries. So, yeah. Not super helpful.

To avoid this it’s just better to make an array like this:

let numberArray = [10]

The bracket notation creates the array, you know what’s in it, and nothing gets all confused. Much better, much cleaner, much less headache.