Here are the next five solutions:
Sum All Odd Fibonacci Numbers
My first attempt at this resulted in the following:
function sumFibs(num) {
let fibs = [1, 1];
let counter = 0;
while (counter <= num) {
fibs.push(fibs[counter] + fibs[counter + 1]);
counter++;
}
let subTotal = 0;
fibs.map(item => {
if (item <= num) {
if (item %2 !== 0) subTotal += item;
}
});
return subTotal;
}
First, this creates a list of fibonacci numbers up to and including the given number. Then it checks each of those numbers to see if it is less than the number given and if the it is odd. If it is less than the given number and odd, it adds it to the subtotal
variable. This feels more straightforward, yet possibly not as efficient, as the basic solution in the fCC Guide for Sum All Fibonacci Numbers. The intermidiate solution in the guide lead me to this, using reduce()
, which is probably more appropriate here:
function sumFibs(num) {
let fibs = [1];
let counter = 1;
while (counter <= num) {
fibs.push(counter);
counter = fibs[fibs.length - 1] + fibs[fibs.length - 2];
}
return fibs.reduce((prev, cur) => cur % 2 !== 0 ? prev + cur : prev)
}
Sum All Primes
This one was difficult for me, specifically the part of trying to get a list of prime numbers. I used a StackOverflow answer to generate the primes ā which is similar to the basic solution in the fCC Guide ā and then it was a matter of getting the sum of those numbers:
function sumPrimes(num) {
let tempArr = [];
let i, j, primes = [];
for (i = 2; i <= num; ++i) {
if (!tempArr[i]) {
// i has not been marked -- it is prime
primes.push(i);
for (j = i << 1; j <= num; j += i) {
tempArr[j] = true;
}
}
}
return primes.reduce((prev, next) => prev + next);
}
The intermediate and advanced solutions in the fCC Guide for Sum All Primes are interesting and worth looking at.
Smallest Common Multiple
I needed to rely on StackOverflow again for this one, this time to figure out the greatest common denominator.
function smallestCommons(arr) {
let sortedArr = arr.sort((a, b) => a - b);
let fullArr = [];
// Create an array with all necessary numbers
let counter = sortedArr[0];
while (counter <= sortedArr[1]) {
fullArr.push(counter);
counter++;
}
// Borrowed from https://stackoverflow.com/a/17136781
function gcd(...fullArr) {
return fullArr.reduce((acc, cur) => cur === 0 ? acc : gcd(cur, acc % cur));
}
return fullArr.reduce((acc, cur) => Math.abs(acc * cur) / gcd(acc, cur));
}
Drop It
Now that weāre back to less math-heavy challenges, I was able to solve this challenge using map()
, indexOf()
, and slice()
:
function dropElements(arr, func) {
let tempArr = arr.map(item => func(item));
return tempArr.indexOf(true) !== -1 ? arr.slice(tempArr.indexOf(true)) : [];
}
This first creates a temp array with true
/false
values based on whether or not the item in the given array is true or false in the given function. Then, if the temp array has a true value, it finds the index of the first ātrueā value and slices the given array at that index, returning everything thereafter. If the temp array doesnāt have a true value, it returns an empty array.
The fCC Guide for Drop It has some other ways to solve this challenge.
The basic solution checks the first element of the given array with the given function. If the function returns true, the loop stops and the array is returned from that point. If the function returns false, the first element of the array is removed using shift()
:
function dropElements(arr, func) {
var times = arr.length;
for (var i = 0; i < times; i++) {
if (func(arr[0])) {
break;
} else {
arr.shift();
}
}
return arr;
}
The intermediate solution is short and sweet. It relies on slice()
and the findIndex()
function. The findIndex()
function returns the index of the element that returns true from the given function. findIndex()
will return -1
if it doesnāt find an element that returns true, so the ternary operator is used to check for that and either return a sliced array from the first true element to the end of the array or a sliced array that returns an empty array:
function dropElements(arr, func) {
return arr.slice(arr.findIndex(func) >= 0 ? arr.findIndex(func): arr.length, arr.length);
}
The advanced solution is also short and sweet, but this time using a while loop
and shift
:
function dropElements(arr, func) {
while(arr.length > 0 && !func(arr[0])) {
arr.shift();
}
return arr;
}
This is similar to the basic solution in that it removes the first element if itās false, but it uses a while loop and doesnāt need to use break
to end the loop when an element thatās true is found.
Steamroller
My initial solution is pretty close to the basic solution in the fCC Guide for Steamroller in that I create a function and then use recursion to keep sending arrays into that function until there are no arrays left:
function steamrollArray(arr) {
let result = [];
const tempFunc = (inArr) => {
inArr.map(item => Array.isArray(item) ? tempFunc(item) : result.push(item));
}
tempFunc(arr);
return result;
}
The intermediate solution is really good. It takes advantage of the spread operator
as well as some()
. It first initializes a variable named flat
that flattens the first level of the array. Then, it uses some()
to check for the existence of another array. If another array exists, it recursively runs the function again. If and when another array doesnāt exist, it returns the flattened array in the flat
variable.
function steamrollArray(arr) {
let flat = [].concat(...arr);
return flat.some(Array.isArray) ? steamrollArray(flat) : flat;
}