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]]);

06 – Title Case a Sentence.js

Bonfire: Title Case a Sentence

Challenge: http://www.freecodecamp.com/challenges/bonfire-title-case-a-sentence

Return the provided string with the first letter of each word capitalized. Make sure the rest of the word is in lower case.

For the purpose of this exercise, you should also capitalize connecting words like “the” and “of”.

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

Here are some helpful links:
String.charAt()
http://ironion.com/bonfire-title-case-a-sentence/

Solution:

function titleCase(str) { // Return str with the first letter of each word capitalized.
	var titleStr = str.split(" "); // splits array into words at each space

        // loops through the words and changes first letter to uppercase, second to lowercase
	for (var i = 0; i < titleStr.length; i++) {
		titleStr[i] = titleStr[i].charAt(0).toUpperCase() + titleStr[i].slice(1).toLowerCase();
	}

  return titleStr.join(" "); // puts each word back into the array
}

titleCase("I'm a little tea pot");

05 – Find the Longest Word in a String.js

Bonfire: Find the Longest Word in a String

Challenge: http://www.freecodecamp.com/challenges/bonfire-find-the-longest-word-in-a-string

Return the length of the longest word in the provided sentence.
Your response should be a number.

Helpful Resources: http://ironion.com/bonfire-find-the-longest-word-solution/

Solution:

function findLongestWord(str) {
	var splitStr = str.split(" "); // split str into an array of substrings
	var longestWord = ""; // used to compare each word and return the longest
	for (var i=0; i < splitStr.length; i++){ // loop through the array
 		if (splitStr[i].length > longestWord.length) { // compare the word
			longestWord = splitStr[i]; // assign longest word to longestWord var
		}
	}
	return longestWord.length; // returns the longest word
}

findLongestWord("The quick brown fox jumped over the lazy dog");

04 – Check for Palindromes.js

Bonfire: Check for Palindromes

Challenge: http://www.freecodecamp.com/challenges/bonfire-check-for-palindromes

Return true if the given string is a palindrome. Otherwise, return false.

A palindrome is a word or sentence that’s spelled the same way both forward and backward, ignoring punctuation, case, and spacing.
You’ll need to remove all non-alphanumeric characters (punctuation, spaces and symbols) and turn everything lower case in order to check for palindromes.

We’ll pass strings with varying formats, such as “racecar”, “RaceCar”, and “race CAR” among others.

Helpful Resources:
(1) http://ironion.com/bonfire-reverse-a-string-solution/
(2) https://github.com/Rafase282/My-FreeCodeCamp-Code

Solution:

function palindrome(str) {
  // converts to lower case, strips out spacing, capitalization and punctuation
  var stripStr = str.toLowerCase().replace(/[.,?:;\/() _-]/g, '');
  // reverses stripped string
  var checkStr = stripStr.split("").reverse().join("");
  // if strings match, return true. if not, false.
  return stripStr === checkStr;
}

palindrome("eye");
palindrome("not a palindrome");
palindrome("0_0 (: /-\ :) 0-0");
palindrome("race car");
palindrome("never odd or even");

03 – Factorialize a Number.js

Bonfire: Factorialize a Number

Challenge: http://www.freecodecamp.com/challenges/bonfire-factorialize-a-number

Return the factorial of the provided integer.

If the integer is represented with the letter n, a factorial is the product of all positive integers less than or equal to n.
Factorials are often represented with the shorthand notation n!
For example: 5! = 1 * 2 * 3 * 4 * 5 = 120

*1 In laymans terms, when you factorialize a number, you are multiplying that number by each consecutive number minus one.

Helpful Resources:
1 http://ironion.com/bonfire-factorialize-a-number-solution/
2 https://github.com/Rafase282/My-FreeCodeCamp-Code/

Solution 1:

function factorialize(num) {
	var factors = []; // Create an array to hold factors
		while (num>1) { // do this while num is greater than 1 
			for (var i = num; i > 0; i--) { // i = 5; 5>0; decrease i;
				factors.push(i); // Pushes the factors into an array, like [5,4,3,2,1]
			}
			for (var x = num; x > 1; x--) { // x = 5; 5 > 1; decrease x;
				num *= factors[x-1]; // 5 = !5 or 5 * 4 * 3 * 2 * 1
			}
		return num;
		}
	return 1; // if number is 1 or less, return 1
}

Solution 2:

function factorialize(num) {
	if (num <= 1) { // if number is less than or equal to 1, return 1
		return 1;
	} else {
		return num * factorialize(num-1); // I don't really understand this...
		// Still reading about how it works at resource 1 listed above
	}
}

factorialize(5);

02 – Reverse a String.js

Bonfire: Reverse a String

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

Reverse the provided string.

You may need to turn the string into an array before you can reverse it. Your result must be a string.

Helpful Resources:
http://ironion.com/bonfire-reverse-a-string-solution/

Solution:

function reverseString(str) {
 // split() returns an array on which reverse() and join() can be applied
 var newStr = str.split("").reverse().join("");
 return newStr;
 }

reverseString("Greetings from Space Camp!");