Week 4

Pamela White
4 min readJul 23, 2021

--

Describe one thing you’re learning in class today.

Today in class, we saw how tests are created. Tests are for us (humans) to know if what we’re doing is working as expected. My “aha” moment with tests came after class, when I learned how important it is to look at what the test is assessing, rather than the description

The “description” section is solely for humans. It tells us, in human language, what the test should assess. It’s as if you’re handing out a math test and you say, “This is your multiplication fact fluency test”. The student now knows that they should expect to be tested on their fluency in multiplication facts.

Let’s say the student gets the test and sees a bunch of problems with addition signs. What should they do? It could be that the teacher misspoke or was unclear, or the student misunderstood what the teacher said. Regardless, the student should look at the problems and solve them.

This is what happened with one of the unit tests that had been written for our Master Mind project. The first test was described as “should register a guess and generate hints”. For the life of me, I wasn’t sure what this meant. Another test already tested if I had generated hints correctly. I should have just looked at what the test was asserting, which had to do with the length of the board. The test was assessing if the guess was printed to the board. In the description the “generate hint” part was redundant (another test tested that), and the description was backward (the generate hint part happens first and it is registered second). The order of the description (with “register a guess” first), caused me to misunderstand what was meant by “register”; I was expecting “register” to happen before “generate hint”. If I had just looked at what the test asserted (board.length, 1), I might have realized it was testing to see if the board printed correctly.

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

A forEach() loop iterates over each element in an array. That’s all it does. It doesn’t return anything. It just looks at them all. Whatever you want it to do needs to be assigned in the callback function. There is the most flexibility in using forEach(), because you, the programmer, need to tell it what you want it to do with the elements it iterates over.

A map() loop iterates over each element in an array and changes each element, returning the array of new elements. The callback function tells how to change each element. You only use this when you want to return an array in which each element is altered.

Describe event bubbling

Event bubbling describes the pattern in which events are triggered. To capture an event, we go from big to small (outermost to innermost). Event bubbling is the opposite of this, the innermost event is triggered first, and then systematically, the events in the next nested event happen all the way to the top.

What is the definition of a higher-order function?

A higher order function is one that uses a callback function as one of it’s parameters.

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

A template literal is using backticks to contain a string, and the ${} to contain expressions or variables within that string. One distinct advantage to using this syntax is readability.

const bananasPrice = 2

const applesPrice = 1

With template literals:

` Apples cost ${applesPrice} each. Bananas cost ${bananasPrice} each. To eat both, I need to add the prices using a plus “+” sign. The result of bananasPrice + applesPrice = ${bananasPrice + applesPrice}`

Without

“Apples cost “ + applesPrice + “ each. Bananas cost “ + bananasPrice + “ each. To eat both, I need to add the prices using a plus ‘+’ sign. The result of bananasPrice + applesPrice = “ + (bananasPrice + applesPrice).”

What is an associative array in JavaScript?

JavaScript does not support associative arrays. Changing indexes to names, rather than numbers, results in an object. The object has keys with the names that you assigned each index. Therefore, you cannot use the “associative array” as you would actual arrays.

Why should you never use new Array in JavaScript?

You should use brackets [] to create new arrays in JavaScript, rather than new Array. Using brackets is more succinct than new Array. Usually, fewer characters is better than more when coding. This is especially true when the shorter version is also more recognizable and understandable. This is the case for [] over new Array. In reading JavaScript, the brackets are more recognizable as an array than new Array and a constructor. Finally, it seems that there is little to no advantage to using new Array.

--

--