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