Tech Notes... 21062023
Tech Notes... 21062023
// Step 2. Initiate a variable that will hold the length of the longest word
var longestWord = 0;
/* Reduce process
currentWord longest currentWord.length longest.length if(currentWord.length >
longest.length)? var longestWord
"The" "" 3 0 yes "The"
"quick" "The" 5 3 yes "quick"
"brown" "quick" 5 5 no "quick"
"fox" "quick" 3 5 no "quick"
"jumped" "quick" 6 5 yes "jumped"
"over" "jumped" 4 6 no "jumped"
"the" "jumped" 3 6 no "jumped"
"lazy" "jumped" 4 6 no "jumped"
"dog" "jumped" 3 6 no "jumped"
*/
// Step 3. Return the length of the longestWord
return longestWord.length; // var longestWord = "jumped"
// longestWord.length => "jumped".length => 6
}
function largestOfFour(arr) {
// Step 1. Create an array that will host the result of the 4 sub-arrays
var largestNumber = [0,0,0,0];
// Step 2. Create the first FOR loop that will iterate through the arrays
for(var arrayIndex = 0; arrayIndex < arr.length; arrayIndex++) {
/* The starting point, index 0, corresponds to the first array */
// Step 3. Create the second FOR loop that will iterate through the sub-arrays
for(var subArrayIndex = 0; subArrayIndex < arr[arrayIndex].length; subArrayIndex+
+) {
/* The starting point, index 0, corresponds to the first sub-array */
largestNumber[arrayIndex] = arr[arrayIndex][subArrayIndex];
For this solution, you’ll use two methods: the Array.prototype.map() method
and the Array.prototype.reduce() method.
SOLUTION
function largestOfFour(mainArray) {
// Step 1. Map over the main arrays
return mainArray.map(function (subArray){ // Step 3. Return the largest numbers of each sub-arrays
=> returns [5,27,39,1001]
// Step 2. Grab the largest numbers for each sub-arrays with reduce() method
return subArray.reduce(function (previousLargestNumber, currentLargestNumber) {
return (currentLargestNumber > previousLargestNumber) ? currentLargestNumber :
previousLargestNumber;
APPROACH 3:
Return the Largest Numbers in a Array With Built-In Functions — with
map() and apply()
For this solution, you’ll use two methods: the Array.prototype.map()
method and the Function.prototype.apply() method.
You can pass an array of arguments to a function by using the apply()
method and the function will execute the items in the array:
A.) Such functions are known as variadic functions, and they can accept
any number of arguments instead of a fixed one.
B.) The Math.max() function returns the largest of zero or more numbers,
and we can pass any number of arguments.
var num = [4,5,1,3];
console.log(Math.max.apply(null, num)); // logs 5
function largestOfFour(mainArray) {
return mainArray.map(function(subArray) {
return Math.max.apply(null, subArray);
});
}
repeatStringNumTimes("abc", 3);
The first is a base case: this is a statement, usually within a conditional clause
like if, that stops the recursion.
The second is a recursive case: this is the statement where the recursive function
is called on itself.
// Step 2. Check if times equals to 1 and return the string itself if it's the case.
if (times === 1) {
return string;
}
if (times > 0) {
return string.repeat(times);
} else {
return "";
}
Truncate a String
Example 1
const str1 = 'This is an example string';
const str2 = 'abc';
document.getElementById("foo").innerHTML =
"<a href='" + pathname +"'>" + trimmedPathname + "</a>"
Finders Keepers
Create a function that looks through an array arr and returns the first element in it that passes
a 'truth test'. This means that given an element x, the 'truth test' is passed if func(x) is true. If
no element passes the test, return undefined.
Method 1
Array.prototype.find()
The find() method returns the first element in the provided array that satisfies the provided
testing function. If no values satisfy the testing function, undefined is returned.
If you need the index of the found element in the array, use findIndex().
If you need to find the index of a value, use indexOf(). (It's similar to findIndex(), but
checks each element for equality with the value instead of using a testing function.)
If you need to find if a value exists in an array, use includes(). Again, it checks each
element for equality with the value instead of using a testing function.
If you need to find if any element satisfies the provided testing function, use some().
const array1 = [5, 12, 8, 130, 44];
console.log(found);
Method 2
The find() method is an iterative method. It calls a provided callbackFn function once for
each element in an array in ascending-index order, until callbackFn returns a truthy value.
find() then returns that element and stops iterating through the array. If callbackFn never
returns a truthy value, find() returns undefined.
CallbackFn is invoked for every index of the array, not just those with assigned values.
Empty slots in sparse arrays behave the same as undefined
Basically, when an arithmetic operations is carried out on booleans, 0 is assigned to false and 1 is
assigned to true.
The variable result is initialized with the value false because the integer 0 will not have any effect
on the final result.
The for loop runs through numbers, n, less than testNo. Using the modulus operator (%), it finds
the remainder when testNo is divided by a number, n.
When the remainder is 0, it returns true and this means that the number, testNo cannot be a prime
number.
If only false is returned as n reaches 1, this means that the number, testNo is a prime number.
Next, the if loop checks if result is equal to 0 and if testNo is greater than 1.
(It checks if the testNo is greater than 1 because numbers less than or equals to 1 (zero and one)
are not prime numbers.)
If it passes the condition, it returns true meaning that the number, testNo is a prime number.
And if it returns false, it means the number, testNo is not a prime number.
Next, a for loop runs through the values in the array, arr and checks if they pass the isPrime() test.
It returns false if the number arr[n] is not a prime number and it returns true if the number arr[n]
is a prime number.