16 – Where do I belong.js

Bonfire: Where do I belong

Challenge: http://www.freecodecamp.com/challenges/bonfire-where-do-i-belong

Return the lowest index at which a value (second argument) should be inserted into an array (first argument) once it has been sorted.

For example, where([1,2,3,4], 1.5) should return 1 because it is greater than 1 (index 0), but less than 2 (index 1).

Likewise, where([20,3,5], 19) should return 2 because once the array has been sorted it will look like [3,5,20] and 19 is less than 20 (index 2) and greater than 5 (index 1).

Remember to use Read-Search-Ask if you get stuck. Write your own code.

Here are some helpful links:
Array.sort()
http://wulkan.me/bonfire-where-do-i-belong/

Solution:

function where(arr, num) {
  // Find my place in this sorted array.
  arr.push(num);
  var sortArray = arr.sort(function(a,b){
    return a-b;
  });
  var newArray = sortArray.indexOf(num);
  return newArray;
}

where([40, 60], 50);

15 – Seek and Destroy.js

Bonfire: Seek and Destroy

Challenge: http://www.freecodecamp.com/challenges/bonfire-seek-and-destroy

You will be provided with an initial array (the first argument in the destroyer function), followed by one or more arguments. Remove all elements from the initial array that are of the same value as these arguments.

Remember to use Read-Search-Ask if you get stuck. Write your own code.

Here are some helpful links:
Arguments object
Array.filter()
http://wulkan.me/bonfire-seek-and-destroy/

Solution:

function destroyer(arr) {
  // Remove all the values
  var newArray = arguments[0];
  var removeArgs = [];
  for (i=1; i < arguments.length; i++){
    removeArgs.push(arguments[i]);
  }
  var final = newArray.filter(function(value){
    return removeArgs.indexOf(value) === -1;
  });
  return final;
}

destroyer([1, 2, 3, 1, 2, 3], 2, 3);

14 – Falsy Bouncer.js

Bonfire: Falsy Bouncer

Challenge: http://www.freecodecamp.com/challenges/bonfire-falsy-bouncer

Remove all falsy values from an array.

Falsy values in javascript are false, null, 0, “”, undefined, and NaN.

Remember to use Read-Search-Ask if you get stuck. Write your own code.

Here are some helpful links:
Boolean Objects
Array.filter()
http://wulkan.me/bonfire-falsey-bouncer/

Solution:

function bouncer(arr) {
  // Don't show a false ID to this bouncer.
  return arr.filter(Boolean); // uses filter to get rid of false values
}

bouncer([7, "ate", "", false, 9]);

13 – Mutations.js

Bonfire: Mutations

Challenge: http://www.freecodecamp.com/challenges/bonfire-mutations

Return true if the string in the first element of the array contains all of the letters of the string in the second element of the array.
For example, [“hello”, “Hello”], should return true because all of the letters in the second string are present in the first, ignoring case.

The arguments [“hello”, “hey”] should return false because the string “hello” does not contain a “y”.

Lastly, [“Alien”, “line”], should return true because all of the letters in “line” are present in “Alien”.

Remember to use Read-Search-Ask if you get stuck. Write your own code.

Here are some helpful links:
String.indexOf()
http://wulkan.me/bonfire-mutations/

Solution:

function mutation(arr) {
  var stringOne = arr[0].toLowerCase();
  var stringTwo = arr[1].toLowerCase();
  for (var i=0; i<stringTwo.length; i++){
    var check = stringOne.indexOf(stringTwo[i]);
    if (check === -1) {
      return false;
    }
  }
  return true;
}

mutation(["hello", "hey"]);

12 – Slasher Flick.js

Bonfire: Slasher Flick

Challenge: http://www.freecodecamp.com/challenges/bonfire-slasher-flick

Return the remaining elements of an array after chopping off n elements from the head.

The head meaning the beginning of the array, or the zeroth index

Remember to use Read-Search-Ask if you get stuck. Write your own code.

Here are some helpful links:
Array.slice()
Array.splice()
http://wulkan.me/bonfire-slasher-flick/

Solution:

function slasher(arr, howMany) {
  // it doesn't always pay to be first
  return arr.slice(howMany);
}

slasher([1, 2, 3], 2);

11 – Chunky Monkey.js

Bonfire: Chunky Monkey

Challenge: http://www.freecodecamp.com/challenges/bonfire-chunky-monkey

Write a function that splits an array (first argument) into groups the length of size (second argument) and returns them as a multidimensional array.

Remember to use Read-Search-Ask if you get stuck. Write your own code.

Here are some helpful links:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/push
http://wulkan.me/bonfire-chunky-monkey/

Solution:

function chunk(arr, size) {
	var myArray = [];
	var i=0;
	while (i < arr.length) {
		myArray.push(arr.slice(i, i+=size));
	}
	return myArray;
}

chunk(["a", "b", "c", "d"], 2);

10 – Truncate a string.js

Bonfire: Truncate a string

Challenge: http://www.freecodecamp.com/challenges/bonfire-truncate-a-string

Truncate a string (first argument) if it is longer than the given maximum string length (second argument). Return the truncated string with a “…” ending.

Note that the three dots at the end add to the string length.
If the num is less than or equal to 3, then the length of the three dots is not added to the string length.

Remember to use Read-Search-Ask if you get stuck. Write your own code.

Here are some helpful links:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/slice
http://ironion.com/bonfire-truncate-string-solution/
https://github.com/FreeCodeCamp/FreeCodeCamp/wiki/bonfire-truncate-a-string

Solution:

function truncate(str, num) {
  // Clear out that junk in your trunk
  if (str.length<=num) {
    return str;
  } else if (num<=3) {
    return str.slice(0,num) + "...";
  } else {
    return str.slice(0,num-3) + "...";
  }
}

truncate("A-tisket a-tasket A green and yellow basket", 11);

09 – Repeat a string repeat a string.js

Bonfire: Repeat a string repeat a string

Challenge: http://www.freecodecamp.com/challenges/bonfire-repeat-a-string-repeat-a-string

Repeat a given string (first argument) n times (second argument). Return an empty string if n is a negative number.

Remember to use Read-Search-Ask if you get stuck. Write your own code.

Here are some helpful links:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String
http://ironion.com/bonfire-repeat-a-string-solution/

Solution:

function repeat(str, num) {
  // repeat after me
  if (num < 0) {
    return "";
  } else {
    return str.repeat(num);
  }
  return str;
}

repeat("abc", 3);

08 – Confirm the Ending.js

Bonfire: Confirm the Ending

Challenge: http://www.freecodecamp.com/challenges/bonfire-confirm-the-ending

Check if a string (first argument) ends with the given target string (second argument).

Remember to use Read-Search-Ask if you get stuck. Write your own code.

Here are some helpful links:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/substr
http://ironion.com/bonfire-confirm-the-ending/

Solution:

function end(str, target) {
// "Never give up and good luck will find you."
// -- Falcor
	if (str.substr(-target.length) == target) {
		return true;
	} else {
		return false; }
}

end("Bastian", "n");

07 – Return Largest Numbers in Arrays.js

Bonfire: Return Largest Numbers in Arrays

Challenge: http://www.freecodecamp.com/challenges/bonfire-return-largest-numbers-in-arrays

Return an array consisting of the largest number from each provided sub-array. For simplicity, the provided array will contain exactly 4 sub-arrays.

Remember, you can iterate through an array with a simple for loop, and access each member with array syntax arr[i] .

Remember to use Read-Search-Ask if you get stuck. Write your own code.

Here are some helpful links:
Comparison Operators
http://ironion.com/bonfire-return-the-largest-numbers-in-an-array/
http://wulkan.me/return-largest-numbers-in-arrays/
https://www.gorkahernandez.com/blog/fcc-bonfire-series-108-return-largest-numbers-in-arrays/

Solution:

function largestOfFour(arr) {
var largestArray = []; // create an empty array to hold largest array
var highestNum = 0; // var that stores the highest number from each array

// use for to loop through arrays and find highest number in each
for (i=0; i<arr.length; i++) {

// finds the highest number using the reduce function
highestNum = arr[i].reduce(function(a,b) {

if (a > b) {
        return a;
      } else { return b; }
    });

// pushes highestNum to largestArray
largestArray.push(highestNum); }

return largestArray; }

largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]);