Good day! Happy Monday! Thank you for the kind words from the last post. It means a lot.
This coming weekend is Alexis’s play. Tommy and I are leaving Friday to head up to her school. I’m excited to be seeing her and I’m also excited to see another of her plays. We will be there for two nights so Tommy can study for his Red Hat exam. I like Lexi’s school library cause you can talk to each other there. At Chris’s school you feel like you have to be quiet as a mouse. Yes, I realize that I’m a quiet person who doesn’t talk much. But having to be quiet as a mouse can make me restless.
There’s an expansion maintenance going on in Final Fantasy 14 for today and tomorrow. I’ve been busy leveling up all the jobs. I believe there are 20 jobs. I’m currently leveling Reaper. I like the Reaper, it’s fun to just go in and shred up the monster. I’m dreading the healing classes. They do have attacks but to solo level them might be painful. I’m certainly won’t bring them into a dungeon, I’m not a healer. Even my red mage main cannot heal. Well, it can but barely. It’s vercure is kind of sad. I already leveled up all the crafting and gathering jobs. Unlike World of Warcraft, one character can possess every job. I do have an alt character that I started to play with Tommy but he hasn’t played in so long. My alt hasn’t been touched in months. I have leveled up my island. Yes, an island. Almost like that of Animal Crossing but not the same either. You can release your minions on the island and grow crops and give your mammets(which are like your island’s servants) jobs to do.
Both dogs are sleeping next to my office chair. They went outside to play after their lunch, then they crashed when they came back inside. It’s like having toddlers. Although Mimi is a senior dog. It’s nice seeing Mimi play. She doesn’t always play but having a puppy around she has been playing more. Mimi is due for a vet check-up. It’s been a little while.
Karissa is learning Spanish. I don’t know much Spanish so I just keep saying the only Spanish I know to her. I really need to learn Tagalog sometime. I’m Filipino, I should know some Tagalog. Well, I know very little but certainly get lost when trying to read my cousins’ posts. I knew more when I lived at home with my mom. She would always speak Tagalog and I just learned what she was saying. She used to get mad when I would tell her that I didn’t understand what she was saying.
I still feel full from lunch. I wish I didn’t feel so guilty about eating lunch. Since I’ve been exercising more, I’ve been hungry more but I try to curb that but it is hard and I give in and eat. Then I feel guilty about eating. Even if it just lunch or a healthy snack.
JavaScript notes…
————————————
Functional programming is all about creating and using non-mutating functions.
The last challenge introduced the concat method as a way to merge arrays into a new array without mutating the original arrays. Compare concat to the push method. push adds items to the end of the same array it is called on, which mutates that array. Here’s an example:
const arr = [1, 2, 3]; arr.push(4, 5, 6);
arr would have a modified value of [1, 2, 3, 4, 5, 6], which is not the functional programming way.
concat offers a way to merge new items to the end of an array without any mutating side effects.
Change the nonMutatingPush function so it uses concat to merge newItem to the end of original without mutating original or newItem arrays. The function should return an array.
function nonMutatingPush(original, newItem) { // Only change code below this line return original.push(newItem); // Only change code above this line } const first = [1, 2, 3]; const second = [4, 5]; nonMutatingPush(first, second);
function nonMutatingPush(original, newItem) { // Only change code below this line return original.concat(newItem); // Only change code above this line } const first = [1, 2, 3]; const second = [4, 5]; nonMutatingPush(first, second);
Array.prototype.reduce(), or simply reduce(), is the most general of all array operations in JavaScript. You can solve almost any array processing problem using the reduce method.
The reduce method allows for more general forms of array processing, and it’s possible to show that both filter and map can be derived as special applications of reduce. The reduce method iterates over each item in an array and returns a single value (i.e. string, number, object, array). This is achieved via a callback function that is called on each iteration.
The callback function accepts four arguments. The first argument is known as the accumulator, which gets assigned the return value of the callback function from the previous iteration, the second is the current element being processed, the third is the index of that element and the fourth is the array upon which reduce is called.
In addition to the callback function, reduce has an additional parameter which takes an initial value for the accumulator. If this second parameter is not used, then the first iteration is skipped and the second iteration gets passed the first element of the array as the accumulator.
See below for an example using reduce on the users array to return the sum of all the users’ ages. For simplicity, the example only uses the first and second arguments.
const users = [ { name: 'John', age: 34 }, { name: 'Amy', age: 20 }, { name: 'camperCat', age: 10 } ]; const sumOfAges = users.reduce((sum, user) => sum + user.age, 0); console.log(sumOfAges);
The console would display the value 64.
In another example, see how an object can be returned containing the names of the users as properties with their ages as values.
const users = [ { name: 'John', age: 34 }, { name: 'Amy', age: 20 }, { name: 'camperCat', age: 10 } ]; const usersObj = users.reduce((obj, user) => { obj[user.name] = user.age; return obj; }, {}); console.log(usersObj);
The console would display the value { John: 34, Amy: 20, camperCat: 10 }.
The variable watchList holds an array of objects with information on several movies. Use reduce to find the average IMDB rating of the movies directed by Christopher Nolan. Recall from prior challenges how to filter data and map over it to pull what you need. You may need to create other variables, and return the average rating from getRating function. Note that the rating values are saved as strings in the object and need to be converted into numbers before they are used in any mathematical operations.
function getRating(watchList) { // Only change code below this line let averageRating; // Only change code above this line return averageRating; } console.log(getRating(watchList));
function getRating(watchList) { // Only change code below this line let nolanMovies = watchList.filter(function(movie) { if (movie.Director === "Christopher Nolan") { return movie; } }); let nolanMovieRatingSum = nolanMovies.reduce(function(total, movie, currentIndex, arr) { total += parseFloat(movie.imdbRating); return total; }, 0) let nolanMovieCount = nolanMovies.length; let averageRating = nolanMovieRatingSum / nolanMovieCount; // Only change code above this line return averageRating; } console.log(getRating(watchList));
Now that you have worked through a few challenges using higher-order functions like map(), filter(), and reduce(), you now get to apply them to solve a more complex challenge.
Complete the code for the squareList function using any combination of map(), filter(), and reduce(). The function should return a new array containing the squares of only the positive integers (decimal numbers are not integers) when an array of real numbers is passed to it. An example of an array of real numbers is [-3, 4.8, 5, 3, -3.2].
Note: Your function should not use any kind of for or while loops or the forEach() function.
const squareList = arr => { // Only change code below this line return arr; // Only change code above this line }; const squaredIntegers = squareList([-3, 4.8, 5, 3, -3.2]); console.log(squaredIntegers);
const squareList = arr => { // Only change code below this line return arr .filter(num => num > 0 && num % parseInt(num) === 0) .map(num => Math.pow(num, 2)); // Only change code above this line }; const squaredIntegers = squareList([-3, 4.8, 5, 3, -3.2]); console.log(squaredIntegers);