-
Notifications
You must be signed in to change notification settings - Fork 340
Bonfire Factorialize a Number
Created by Rafase282
Github | FreeCodeCamp | CodePen | LinkedIn | Website | E-Mail
- Difficulty: 1/5
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 = 120f
Remember to use Read-Search-Ask if you get stuck. Try to pair program. Write your own code.
function factorialize(num) {
return num;
}
factorialize(5);
This problem is very straightforward, yet it has two possible ways to get the solution.
-
for
loops. - Recursion.
You need to take the number and multiply it for one less until you get to one.
You can use a for loop and have a variable store the product of the current value and one less than it.
Remember the control, the last number has to be 1
and the initial number should be minimum 2, if one then return it, otherwise handle the error.
function factorialize(num) {
var factorial = 1;
for (var n = 2; n <= num; n++) {
factorial = factorial * n;
}
return factorial;
}
I decided to use the loop option. Because any number by one is the same number, we don't need to cover one. So we start with two and increase to the number, since the property of multiplication allows us to start in any order it works by multiplying the current number which is 1 by default by the value of n.
Thanks for visiting, if you like this please feel free to star my repo, follow me or even contact me about contributing as it will be a lot of work and having help would be cool.
- HTML5 and CSS
- Responsive Design with Bootstrap
- Gear up for Success
- jQuery
- Basic JavaScript
- Object Oriented and Functional Programming
- Basic Algorithm Scripting
- Basic Front End Development Projects
- Intermediate Algorithm Scripting
- JSON APIs and Ajax
- Intermediate Front End Development Projects
- Claim Your Front End Development Certificate
- Upper Intermediate Algorithm Scripting
- Automated Testing and Debugging
- Advanced Algorithm Scripting
- AngularJS (Legacy Material)
- Git
- Node.js and Express.js
- MongoDB
- API Projects
- Dynamic Web Applications
- Claim Your Back End Development Certificate
- Greefield Nonprofit Project 1
- Greefield Nonprofit Project 2
- Legacy Nonprofit Project 1
- Legacy Nonprofit Project 2
- Claim your Full Stack Development Certification
- Whiteboard Coding Interview Training
- Critical Thinking Interview Training
- Mock Interview 1
- Mock Interview 2
- Mock Interview 3