I’ve figured out how to write that takes two arrays and returns a new array containing only the elements that are common between them:
function findCommonElements(arr1, arr2) { return arr1.filter(element => arr2.includes(element)); }
The console.log works:
console.log(arrayIntersection([1, 2, 3, 4], [3, 4, 5, 6])); // [3, 4]
The next problem is to write a function that takes a nested array and returns a single flattened array. Ok, I have no clue what a flattened array is, so let me look that up.
Thanks to Google, I have the answer to what a flattened array is. A flattened array is an array where all nested sub-arrays are converted into a single-level array. In other words, it removes the hierarchy and brings all elements into one continuous list.
Example:
Nested Array (Before Flattening)
const nestedArray = [1, [2, 3], [4, [5, 6]]];
Flattened Array (After Flattening)
[1, 2, 3, 4, 5, 6]
To flatten an array, we will use the flat() method. The .flat() method can flatten an array to a specified depth.
const arr = [1, [2, 3], [4, [5, 6]]]; console.log(arr.flat()); // Output: [1, 2, 3, 4, [5, 6]]
By default, .flat() only flattens one level. To fully flatten, use:
console.log(arr.flat(Infinity)); // Output: [1, 2, 3, 4, 5, 6]
After some time I have come up with a code that does work. Which is crazy cause the answer was in the example all along.
function flattenArray(arr) { return arr.flat(Infinity); }
The console.log they gave was:
console.log(flattenArray([1, [2, [3, 4], 5], 6])); // [1, 2, 3, 4, 5, 6]
The next problem will be to write a function that takes a sentence and returns the longest word. I think I’ve done this function before. We will need to split the sentence into words and sort the words by length to return the longest word. We are going to handle punctuation using Regex. I will explain it because I happen to like Regex:
(/[^\w\s]/g, "")
Here’s a breakdown:
[]: This indicates a character class, meaning we’re looking for any character within the brackets.
^: Inside the character class, ^ means “not” or “negation”.
\w: matches any word character (letters, numbers, and underscores).
\s: matches any whitespace character (space, tab, newline).
g: The g flag at the end means “global,” meaning the replacement should occur for all occurrences, not just the first.
“”: The empty string “” is the replacement value. It means that any character matched by the regular expression will be replaced with nothing, effectively deleting it.
function longestWord(sentence) { const words = sentence.replace(/[^\w\s]/g, "").split(" "); //removing all characters that are not alphanumeric or whitespace from a string, globally and splitting words using spaces as the separator. return words.reduce((longest, current) => current.length > longest.length ? current : longest, ""); //.reduce will iterate through the words and find the longest word. }
The callback function (longest, current) => … takes two parameters:
longest → Keeps track of the longest word found so far.
current → The word currently being checked.
Then it compares the length of current and longest:
current.length > longest.length ? current : longest
If current is longer, it becomes the new longest.
Otherwise, longest remains unchanged.
The initial value (“”) ensures that the function starts with an empty string before checking words.
Thats a lot to remember but I’m understanding how this is working. On Monday we will do anagrams. I need to check if two strings are anagrams. Two strings are considered anagrams if they contain the exact same characters with the same frequency, but possibly in a different order. The words “listen” and “silent” are anagrams because they both contain the same letters, just rearranged. So we are going to need to convert both strings to lowercase and sort their characters alphabetically. Then, if the sorted versions of both strings are the same, they are anagrams.
function isAnagram(str1, str2) { //code goes here )
Alex just came home and brought me the rest of the candy he had been eating: Starburst Sour Gummies. They are very sweet, not exactly sour, but definitely on the sweet side. I’m finding it a bit challenging to eat them; I’ve had two pieces and there are two pieces left. I might just end up throwing them away.
Lexi called and mentioned that she’s having a great time on her outing. She presented today and is considering getting some homework done before watching other presentations. I’m glad she’s enjoying herself and staying safe. I’ll get to see her on Saturday.
I don’t have much to share today. I completed my coding work and thought about doing laundry, but I think I’ll save that for tomorrow. Tommy has band practice tonight, so I thought I’d fill in my planner and might play a video game later. For lunch today, I had leftover spaghetti, and I plan to make myself some matcha tea in a bit.
Aww, Tommy doesn’t feel that great. The wind is really kicking up out there and his allergies are acting up. So no band practice tonight. He’s on his way home now.





