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