JavaScript Algorithms - 8 - Factorial of a Number

  Рет қаралды 65,946

Codevolution

Codevolution

Күн бұрын

Пікірлер
@khairiyusoff5040
@khairiyusoff5040 2 жыл бұрын
function factorial(n) { let output = 1; for (let i = n; i >= 1; i--) { output *= i; } return output; } console.log(factorial(0)); //1 console.log(factorial(1)); //1 console.log(factorial(4)); //24 console.log(factorial(6)); //720
@YakovFn
@YakovFn 7 ай бұрын
I did it the same
@paulbird2772
@paulbird2772 2 жыл бұрын
I really like recursive approach for this: const factorial = (n) => { return n
@isaacdruin
@isaacdruin 2 жыл бұрын
For this, you still have to write the factorial function as well, no? Same O and this would be less intuitive imo.
@paulbird2772
@paulbird2772 2 жыл бұрын
@@isaacdruin Nothing extra needed, that is the function. If you never seen recursion before, then I agree it is less intuitive than a for loop. I highly recommend pasting my one liner into a browser console and trying it out. It is the best example to learn what recursion is because it is so simple.
@manjunatha5a
@manjunatha5a 2 жыл бұрын
I think he is keeping recursion approach for later.
@YakovFn
@YakovFn 7 ай бұрын
Yea, me too! In maths you can say 5! = 5*4! = 5*4*3!...
@SuperYoman100
@SuperYoman100 6 ай бұрын
That'd be O(2^n) TC. Much worse.
@mohamedothman3637
@mohamedothman3637 2 жыл бұрын
function fib(n){ let num = 1 for(let i =0; i
@miladcodes
@miladcodes 2 жыл бұрын
Here is another solution const factorial = (n) => { let factorial = 1 for (let i = n; i > 1; i--) { factorial *= i } return factorial }
@norineoo6417
@norineoo6417 Жыл бұрын
This is my solution: function factorial(n) { let result = 1 for (let i=0; i
@akaris8042
@akaris8042 2 жыл бұрын
Using recursion makes more sense to me function factorial(nnum){ //base case if(num == 0 || num == 1){ return 1; //recursive case }else{ return num * factorial(num - 1); } } / console.log(factorial(5)) //120 console.log(factorial(4)) //24 console.log(factorial(6)) //720
@natasdabsi1138
@natasdabsi1138 6 ай бұрын
fixed, function factorial(num){ //base case if(num == 0 || num == 1){ return 1; //recursive case }else{ return num * factorial(num - 1); } } console.log(factorial(5)) //120 console.log(factorial(4)) //24 console.log(factorial(6)) //720
@srudsalam6970
@srudsalam6970 Жыл бұрын
// using loop function factorial(n) { let fac = 1; for(let i=2; i < n; i++) { fac *= i; } return fac; } // using recursive function factorial(n) { if (n === 0) return 1; return n * factorial(--n); } console.log('0: ', factorial(0)); console.log('1: ', factorial(1)); console.log('2: ', factorial(2)); console.log('3: ', factorial(3)); console.log('4: ', factorial(4)); console.log('5: ', factorial(5));
@DutchmanRadio
@DutchmanRadio Жыл бұрын
I also wanted to write a for loop despite the comments here, I enjoyed this exercise and the explanation a lot
@prachi_arora
@prachi_arora Ай бұрын
Thank you so much🙏
@DantesSagan
@DantesSagan 2 жыл бұрын
Thanks, I've got a simple solution with for loop: function factorial(n){ let result = 1; for(let i = n; i > 1; i--){ if(n === 0){ return result; } else { result *= i } } return result } let result = factorial(0); let resultTwo = factorial(4); let resultThree = factorial(5); console.log(result, resultTwo, resultThree);
@narasimhakamath3808
@narasimhakamath3808 2 жыл бұрын
Why do you want to check for n === 0 in every iteration? You can return 1 if n === 0 || n === 1 at the top of the function itself.
@podcasttakes
@podcasttakes 6 ай бұрын
function factorial(n) { if (n === 0 || n === 1) { return 1; } let result = 1; // Initialize result to 1 for (let i = 2; i
@sadhruva3848
@sadhruva3848 Жыл бұрын
function fact(n) { let result = 1; while (n >= 1) { res *= n; n - - } return result }
@bayasahamed1815
@bayasahamed1815 7 ай бұрын
function factorial(num) { let factor = 1; for (let i = num; i > 1; i--) { factor = factor * i; } return factor; }
@AdarshSingh-mm9lh
@AdarshSingh-mm9lh 2 жыл бұрын
I gone through this way... function factorial(n) { let result = n; if (!result) return 1; for (let i = n; i > 1; i--) { result *= (i - 1); } return result; }
@natasdabsi1138
@natasdabsi1138 6 ай бұрын
function factorial(n) { var y = []; var x = 1; for (var i = 1; i
@waleedcodes1422
@waleedcodes1422 2 жыл бұрын
I did this solution for the algorithm: function factorial(number) { let result = 1 while(number > 1) { result *= number number-- } return result } console.log(factorial(5))
@armoredchimp
@armoredchimp Жыл бұрын
I have zero experience with data structures/algorithms after the past 9 months of self-studying programming and cloud computing. Just started learning these and I am not good at them! Managed to get an almost-correct answer for this and fibonnaci on my own, but both were not quite correct. I'm going to do 1-2 of these at minimum every day until I get much better.
@rohitrout6450
@rohitrout6450 2 жыл бұрын
solve it when contraints are n
@premsingh6967
@premsingh6967 Жыл бұрын
@dsuppal9059
@dsuppal9059 9 ай бұрын
In your case i think if you pass 0 or 1 then code won't goes into the loop. It directly goes to the return statement; So Big O notation for the same will be O(1)
@YakovFn
@YakovFn 7 ай бұрын
Big O means the worst case scenario, you are talking about the best
@Ayoubmajid-uu9yv
@Ayoubmajid-uu9yv 10 ай бұрын
// solve the problem of factorial : function factorial(n) { if (n == 0 || n == 0) return 1; let fact = 1; for (let i = 2; i
@NadirAli-kn7sl
@NadirAli-kn7sl 2 жыл бұрын
function factorial(n) { let result = 1 if(n!==0){ for(let i=n;i>=1;i--){ result*=i } } return result }
@vaibhavsharma4743
@vaibhavsharma4743 Жыл бұрын
let factorial = (n)=>{ let result = 1; for(let i=2;i
@simdanofficial
@simdanofficial 4 ай бұрын
function factorial(n: number) { let result = 1; while (n > 0) { result = result * n; n--; } return result; }
@alcidesJoyable
@alcidesJoyable Ай бұрын
loved it! I got lost at last part - Big-O?
@vaibhavsharma4743
@vaibhavsharma4743 Жыл бұрын
const factorial = (n) => { return n
@ronaldomaia
@ronaldomaia 7 ай бұрын
function Factorial(n) { if (n === 0) return 1; return n * Factorial(n - 1); }
@andresmoran3569
@andresmoran3569 3 ай бұрын
// Factorial (4) = 4 * 3 * 2 * 1 = 24; // factorial (5) = 5 * 4 * 3 * 2 * 1 = 120; function factorial(n){ let result = 1; for(let i = n; i > 0; i--){ result *= i; } return result; } console.log(factorial(0)); // 1 console.log(factorial(1)); // 1 console.log(factorial(4)); // 24 console.log(factorial(5)); // 120
@yogeshlate4748
@yogeshlate4748 Жыл бұрын
// Normal approach function factorial(number){ let fact = 1; for(let i =number;i>0;i--){ fact= fact*=i; } return fact; } console.log(factorial(4)) // Recursive approach function factorial(number){ if(number === 0){ return 1; } return number * factorial(number-1); } console.log(factorial(5)) 🙂
@legdayenjoyer6000
@legdayenjoyer6000 Жыл бұрын
const factorial = (n) => { let fact = 1 for (let i = 0; i
@badbrother_2023
@badbrother_2023 2 жыл бұрын
If you replace the multiplication sign with an addition sign and n becomes 3 you will get 6 but the math begs to differ as you will get 7
@shadowmixer
@shadowmixer 2 жыл бұрын
const factorial = n => { fac = 1; for (let i = n; i > 0; i--) { fac *= i; } return fac; }
@grishaghlijyan2333
@grishaghlijyan2333 10 ай бұрын
@Codevolution - as we are starting the iteration from 2, isn't the complexity of this algorithm O(n-1) instead of O(n)?
@abhayjeetsingh4211
@abhayjeetsingh4211 6 ай бұрын
Constants can be neglected
@jerbparagas3924
@jerbparagas3924 10 ай бұрын
Here is my implementation with while & recursion // recursion const fact = ( n : number ) : number => { return n === 0 ? 1 : n * fact(n - 1) } // while loop - i found while loop much simpler const factLoop = ( n : number ) : number => { let result = 1; while(n > 1) result *= n-- return result; }
@jmeahra
@jmeahra 2 жыл бұрын
amazing keep uploading, one question are you going to use recusion again after completion of this math algos?
@shujaatali8414
@shujaatali8414 10 ай бұрын
I have start from 1 what is the disadvantage of i = 1 instead of 2?
@wedho_lifeInquisitor
@wedho_lifeInquisitor 3 ай бұрын
why is i always equal to 2? what is i's relationship with n?
@projectdevelopers9639
@projectdevelopers9639 Жыл бұрын
var a =0 var factorial =1 while (a > 1) { factorial= factorial * a a-- }
@jericmoreno7088
@jericmoreno7088 Жыл бұрын
my answer const Factorial = (n) => { let total = 1; for(let i = n; i > 0; i--){ total *= i } return total } console.log(Factorial(4)) console.log(Factorial(5))
@Mark-iv5dl
@Mark-iv5dl 2 жыл бұрын
function factorialNumber(n) { let sum = n; if (n === 0) { return 1; } for (i = n - 1; i >= 1; i--) { sum *= i; } return sum; }
@MythicEcho
@MythicEcho 2 жыл бұрын
My solution function getFactorial(n){ let result=1; if(n===0){ return 1; }else if(n1; i--){ result = result*i; } } return result; }
@Lafsha
@Lafsha Жыл бұрын
const fact=num=>num==0?1:num*fact(num-1)
@kamilturek3788
@kamilturek3788 2 жыл бұрын
Hello. If I add if checking if n === 0 and else for other values of n's, and if I set the argument to 0, is the complexity O(1)? let factorial = function (n) { let total = 1; if (n === 0) { return 1; } else { for (let i = n; i > 0; i--) { total *= i; } } return total; };
@ishantkushwaha534
@ishantkushwaha534 2 жыл бұрын
Yes
@c.5879
@c.5879 Жыл бұрын
function getFactorial(n:number){ let factorial = 1 for(let i=1; i
@dsuppal9059
@dsuppal9059 9 ай бұрын
Here is my code function factorial(n){ let i=n; let value = 1; while(i!=0){ value *= i i-- } return value } console.log(factorial(1)) // Bog O = O(n)
JavaScript Algorithms - 9 - Prime Number
7:31
Codevolution
Рет қаралды 65 М.
JavaScript Algorithms - 10 - Power of Two
8:53
Codevolution
Рет қаралды 59 М.
Beat Ronaldo, Win $1,000,000
22:45
MrBeast
Рет қаралды 158 МЛН
Гениальное изобретение из обычного стаканчика!
00:31
Лютая физика | Олимпиадная физика
Рет қаралды 4,8 МЛН
She made herself an ear of corn from his marmalade candies🌽🌽🌽
00:38
Valja & Maxim Family
Рет қаралды 18 МЛН
Factorial Program in Java with Recursion #70
9:48
Alex Lee
Рет қаралды 106 М.
JavaScript Algorithms - 4 - Big-O Notation
10:33
Codevolution
Рет қаралды 114 М.
JavaScript Algorithms - 13 - Recursive Factorial of a Number
6:46
Codevolution
Рет қаралды 37 М.
My 10 “Clean” Code Principles (Start These Now)
15:12
Conner Ardman
Рет қаралды 306 М.
Large Language Models explained briefly
8:48
3Blue1Brown
Рет қаралды 957 М.
JavaScript Algorithms - 7 - Fibonacci Sequence
6:09
Codevolution
Рет қаралды 101 М.
Factorial using Recursion | JavaScript
9:01
Telusko
Рет қаралды 43 М.
Why Signals Are Better Than React Hooks
16:30
Web Dev Simplified
Рет қаралды 493 М.
This is the Only Right Way to Write React clean-code - SOLID
18:23
Beat Ronaldo, Win $1,000,000
22:45
MrBeast
Рет қаралды 158 МЛН