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!");

01 – Meet Bonfire.js

Bonfire: Meet Bonfire

Bonfires are algorithm challenges that teach you how to think like a programmer…

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

Your goal is to fix the failing test.

First, run all the tests by clicking “Run tests” or by pressing Control + Enter. The failing test is in red. Fix the code so that all tests pass. Then you can move on to the next Bonfire.

Make this function return true no matter what.

Solution:

function meetBonfire(argument) {
 console.log("you can read this function's argument in the developer tools", argument);
 return true; // changed this Boolean from false to true
}

 meetBonfire("You can do this!");