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

Bonfire 30: Sum All Odd Fibonacci Numbers

TASK: Return the sum of all odd Fibonacci numbers up to and including the passed number if it is a Fibonacci number.

function sumFibs(num) {

var numA = 0;
var numB = 1;
var sum = 0;
  
  while (numB <= num) {
   // check if the number is odd
    if (numB %2 !==0) {
   // add to sum
      sum += numB;
    }
   // record added value before changing the num values
    var added = numA + numB;
   
    numA = numB
  // the next Fibonacci number is the sum of the previous two    
  numB = added;
  }
  
 return sum;
}

Bonfire 30: Sum All Odd Fibonacci Numbers

Bonfire 29: Spinal Tap Case

TASK: Convert a string to spinal case. Spinal case is all-lowercase-words-joined-by-dashes.

I’m not particularly proud of the way I got it to work for theCamelCase, but…


function spinalCase(str) {
    
  
    str = str.replace(/([ _-])/g, '-');
    
    if (str.split("-").length === 1) {
      str = str.split(/(?=[A-Z])/).join("-");
    }
  
    return str.toLowerCase();
}

spinalCase('This Is Spinal Tap');

Bonfire 29: Spinal Tap Case

Bonfire 28: HTML entities

TASK: Convert the characters “&”, “<“, “>”, ‘”‘ (double quote), and “‘” (apostrophe), in a string to their corresponding HTML entities.


function convert(str) {
str = str.replace(/&/g,"&amp;");
str = str.replace(/</g,"&lt;");
str = str.replace(/>/g,"&gt;");
str = str.replace(/"/g,"&quot;");
str = str.replace(/'/g,"&apos;");
return str;
}

Bonfire 28: HTML entities