Bonfire break & big changes

While I was busy catching up with the past Bonfire challenges, Free Code Camp went down because some important changes were being made. Some good things came out of that. To name one, I was forced to re-solve the Bonfire puzzles, rather than just copy-paste them. When the FCC site started working again, I found the whole map had been turned upside down, and a bunch of new challenges had been added. I’m now working my way through the Basic Front End Development Projects. I think it’s a good idea to give the chance to start with “practical” projects earlier in the course – it’s quite rewarding. I just completed the first one of the, the personal portfolio page. I didn’t spend much time curating the design, but I’m happy to have manage with relative speed and ease.

The rules for the challenge were:

  1. Don’t look at the example project’s code on CodePen. Figure it out for yourself.
  2. You may use whichever libraries or APIs you need
  3. Reverse engineer the example project’s functionality, and also feel free to personalize it.
  4. Users can access all of the portfolio webpage’s content just by scrolling.
  5. Users can click different buttons that will take me to the portfolio creator’s different social media pages.
  6. Users can see thumbnail images of different projects the portfolio creator has built (if you haven’t built any websites before, use placeholders.)
  7. Bonus: users navigate to different sections of the webpage by clicking buttons in the navigation.

Here‘s what I got!

Bonfire break & big changes

Bonfire 36: Bonfire: Binary Agents

TASK: Return an English translated sentence of the passed binary string.

// divide string into characters
var chars = str.split(" ");
var decimal = 0;
var msg = [];

for (var i=0; i<chars.length; i++) {
// transcode from binary to decimal
decimal = parseInt(chars[i],2);
// decode decimal and push into array
msg.push(String.fromCharCode(decimal));
decimal = 0;
}

return msg.join("");

}

binaryAgent('01000001 01110010 01100101 01101110 00100111 01110100 00100000 01100010 01101111 01101110 01100110 01101001 01110010 01100101 01110011 00100000 01100110 01110101 01101110 00100001 00111111');

Links:

Bonfire 36: Bonfire: Binary Agents

Bonfire 35: Steamroller

TASK: Flatten a nested array. You must account for varying levels of nesting.

I must admit I am still struggling with this stuff, but hey, it’s recursive and it works!
I’m much, much in debt to Jai’s solution on this thread.


function steamroller(arr) {


if (Array.isArray(arr) && arr.length > 0) {
var head = arr[0];
var tail = arr.slice(1);

return steamroller(head).concat(steamroller(tail));
} else {
return [].concat(arr);
}
};


steamroller([1, [2], [3, [[4]]]]);

Bonfire 35: Steamroller

Bonfire 34: Drop it

TASK: Drop the elements of an array (first argument), starting from the front, until the predicate (second argument) returns true.

This one passes the test, but I feel like I cheated, since it does not, strictly speaking, do what was requested.


function drop(arr, func) {

return arr.filter(func);
  
  }
 

drop([1, 2, 3], function(n) {return n < 3; });

I tried hard to write a recursive that would actually better fulfil the task, but I couldn’t make it work. This is an alternative that comes closer to doing what is asked:


function drop(arr, func) {

var solution = [];

for (var i=0; i<arr.length; i++) {
if (func(arr[i])) {solution.push(arr[i]);}
}

return solution;
}


drop([1, 2, 3], function(n) {return n < 3; });

Bonfire 34: Drop it

Bonfire 32: Smallest Common Multiple

TASK: Find the smallest number that is evenly divisible by all numbers in the provided range.

Maths can be daunting;


function smallestCommons(arr) {

// define which value is greater
var min = arr[0];
var max = arr[1];

if (arr[0]>=arr[1]) {
min = arr[1];
max= arr[0]
}

// create array with all values within the range

function range(min, max) {
var arr = [];
for (var i = min; i <= max; i++) {
arr.push(i);
}
return arr;
}

// greatest common denominator formula
function gcd(a, b) {
return !b ? a : gcd(b, a % b);
}

// least common multiple formula
function lcm(a, b) {
return (a * b) / gcd(a, b);
}

// calculate for all values in the range
var multiple = min;
range(min, max).forEach(function(n) {
multiple = lcm(multiple, n);
});

return multiple;
}
smallestCommons([1,13])

Links:

Bonfire 32: Smallest Common Multiple

Bonfire 31: Sum All Primes

TASK: Sum all the prime numbers up to and including the provided number.


function sumPrimes(num) {

var total = 0;
// check if n is prime by dividing n for all numbers up to n-1
function isPrime(x) {
if (x<2) {return false;}

for (y=2; y<x; y++) {
if (x%y===0) {return false;}
}
return true;
}
//loop through numbers up to num
// if they pass the test, add them to the total
for (i=num;i>1;i--){

if (isPrime(i)){
total+=i;}

}

return total;
}


sumPrimes(10);

Bonfire 31: Sum All Primes