100% i did this least efficient way, but it worked[spoilers]

so on the factorial a number challenge i was stuck for a little bit but heres my solution, im fairly sure this is not how its meant to be done but hey it worked definitely looking for same advice to make this better.

 var facArr=[];
 while(num>=1){
   facArr.push(num);
   if(num>1){
   num--;}else{break;}
   
 }
 if(num!=0){
   for(i=0;i<facArr.length;i++){
   num *=facArr[i];
 }
 }else{
   num=1;
 }

Do you want to solve the challenge in a recursive way or iterative way? Because there exists a simple recursive solution which is the following :

function factorial(n) {
  if (n == 0) {
    return 1
  } else {
    return n*factorial(n-1)
  }
}

Or if you prefer a concise version using ES6

const factorial = (n) => n == 0 ? 1 : n*factorial(n-1)

Stick with the loop approach, recursion has a big impact on javascript performance. My fibonacci sequence I had the other day was killing the browser with 2 digit numbers.

That being said, this solution can be done with 1 loop, and no array. I’d go with a for loop. The solution I have is 7 lines.

EDIT: To be fair, fibonacci does a lot more recursion…

As long as it works I would not get stuck trying to make it perfect. My solution is not ideal either,but when I cannot come up with a solution I think might be ideal I just try to logically solve it. I’d be more concerned of not being able to logically solve a problem, as that is certainly harder to overcome.

I do plan do go back later and change those answers, but for now I am more focused on being able to solve challenges and the projects than the ideal code.