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;
}

Advertisements
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

Bonfire 27: Sorted Union

TASK: Write a function that takes two or more arrays and returns a new array of unique values in the order of the original provided arrays.


function unite(arr1, arr2, arr3) {

var finalArr = arr1;


for (i=0; i<arguments.length; i++) {
for (h=0; h<arguments[i].length; h++)
if (finalArr.indexOf(arguments[i][h])<0) {
finalArr.push(arguments[i][h]);
}
}


return finalArr;
}

unite([1, 3, 2], [5, 2, 1, 4], [2, 1]);

Bonfire 27: Sorted Union

Bonfire 25: Missing letters

TASK: Find the missing letter in the passed letter range and return it. If all letters are present in the range, return undefined.


function fearNotLetter(str)
{

// loop through string
for(var i = 1; i < str.length - 1; i ++)
{

// check whether all letter have consequential unicode values
if((str.charCodeAt([i+1]) - str.charCodeAt([i])) != 1)
{

// if there are any gaps, return missing letter
return String.fromCharCode(str.charCodeAt([i]) + 1);
}
}

// else continue until the function returns undefined;
return undefined;
}

fearNotLetter('abce');

Bonfire 25: Missing letters

Bonfire 24: DNA Pairing

TASK: The DNA strand is missing the pairing element. Match each character with the missing element and return the results as a 2d array.

I suspect there are more elegant solutions, but this is what I came up with.


function pair(str) {

var dna = [];
var tempSubArr = [];

for (i=0; i<str.length; i++) {

if (str[i]==="G") {
tempSubArr = [str[i], "C"];
dna.push(tempSubArr);
}

if (str[i]==="C") {
tempSubArr = [str[i], "G"];
dna.push(tempSubArr);
}

if (str[i]==="A") {
tempSubArr = [str[i], "T"];
dna.push(tempSubArr);
}

if (str[i]==="T") {
tempSubArr = [str[i], "A"];
dna.push(tempSubArr);
}



}

return dna;
}

pair("GCG");

Bonfire 24: DNA Pairing