最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Factorial of a number using a while loop in Javascript - Stack Overflow

programmeradmin1浏览0评论

I need help creating the code to find the factorial of a number. The task is to

  1. Create a variable to store your answer and initialize it to one
  2. Create a loop that beings at the given value, fact
  3. Check if fact is one or zero
  4. multiply fact with your answer variable
  5. At the end of the loop decrease fact
  6. Print answer using console.log

The pseudocode is

while(factorial)
  if factorial == 0 or factorial == 1
    break 
  result => result * factorial
  factorial  => factorial - 1

My code below isn't plete because I'm confused by the pseudocode.

function nth_fact(nth){
var a = 1
while(nth_fact)
if (nth_fact == 0 || nth_fact == 1){
    break;
result => result * nth_fact
nth_fact => nth - 1
console.log() 
}
}

I need help creating the code to find the factorial of a number. The task is to

  1. Create a variable to store your answer and initialize it to one
  2. Create a loop that beings at the given value, fact
  3. Check if fact is one or zero
  4. multiply fact with your answer variable
  5. At the end of the loop decrease fact
  6. Print answer using console.log

The pseudocode is

while(factorial)
  if factorial == 0 or factorial == 1
    break 
  result => result * factorial
  factorial  => factorial - 1

My code below isn't plete because I'm confused by the pseudocode.

function nth_fact(nth){
var a = 1
while(nth_fact)
if (nth_fact == 0 || nth_fact == 1){
    break;
result => result * nth_fact
nth_fact => nth - 1
console.log() 
}
}
Share Improve this question edited Feb 25, 2018 at 14:44 Vidovitsch 591 silver badge8 bronze badges asked Feb 25, 2018 at 13:38 HannahHannah 51 silver badge4 bronze badges 3
  • 1 nth_fact is a function, take nth – Jonas Wilms Commented Feb 25, 2018 at 13:40
  • 1 => is an arrow function, you want the assinment operator = – Jonas Wilms Commented Feb 25, 2018 at 13:41
  • What is => doing in your pseudocode? – melpomene Commented Feb 25, 2018 at 13:42
Add a ment  | 

7 Answers 7

Reset to default 3

At first lets examine what went wrong:

var a = 1

What is a? Its definetly not a good name for a variable. Maybe name it to result ? The same applies to nth which should be named factorial and nth_fact which should rather be factorize or sth. You should also always use ; to end a statement.

while(nth_fact)

As your while loop contains multiple statements (the if and the two assignments) you need to open a block here by using { right after the condition. nth_fact refers to the function, you rather want to take factorial here.

 if (nth_fact == 0 || nth_fact == 1){
   break;

Now you open a block statement for the if, but you never close it. So you need another } after the break.

result => result * nth_fact
nth_fact => nth - 1
console.log() 

=> is the arrow function expression, but you want the assignment operator =. Also you need to pass something to console.log, e.g. console.log(result)

All together:

 function factorize(factorial){
   var result = 1;
  while(factorial){
     if (factorial == 0 || factorial == 1){
        break;
     }
     // ?
     factorial = factorial - 1;
     console.log(result);
  }
  return result;
}

That pseudocode is indeed confusing, because what it calls factorial is actually not the factorial -- it's the current value, which the result (which is actually the factorial we're looking for) is multiplied by. Also, if is superfluous, because while already checks for the same condition. So the correct pseudocode would be

currentValue = argument
factorial = 1

while (currentValue > 1)
    factorial = factorial * currentValue
    currentValue = currentValue - 1

// now, 'factorial' is the factorial of the 'argument'

Once you get this sorted out, here's a bonus assignment:

  • create a function range(a, b) that creates an array of numbers from a to b. For example, range(5, 8) => [5, 6, 7, 8]
  • create a function product(array) that multiples array elements by each other. For example, product([2, 3, 7]) => 42
  • write the factorial function using product and range

I solve this this way

function factorial(number) {
  let num = 1;
  let result = 1;
  while (num <= number) {
    result = result * num;
    num++;
  }
  return result;
}
const myNumber = factorial(6);
console.log(myNumber);

function factorial(num) {
         var result = 1
         while (num) {
            if ((num) == 0 || (num) == 1) {
               break;
            } else {
               result = result * num;
               num = num - 1;
            }
         }
         return `The factorial of ${val} is ${result}`
      }

      let val = prompt("Please Enter the number : ", "0");
      var x = parseInt(val);
      console.log(factorial(x));

A Short And Clean Code is :

let number = 5;
let numberFactorial = number;
    while(number > 1){
        numberFactorial = numberFactorial * (number-1);
        number--;
    }
console.log(numberFactorial);

function factorize(factorial) {
    if(factorial == 0 | factorial == 1) {
        return 1
    }
    else{
        var result = factorial;
        while(factorial >= 1 ){

            if(factorial-1 == 0) {
                break
            };

            result = result * (factorial - 1);

            factorial = factorial-1;

            //DEBUG: console.log(factorial + ' ' +  result);
        };
        return(result);
    }
}

If you want more info about functions, can see in my GitHub, good learning! Github: https://github./bennarthurdev/JavaScript/tree/main/FUNCOES

You used the right approach. Just the syntax was wrong. Here it is:

function nth_fact(nth){
var result = 1 ;
while(nth){
  if ((nth) == 0 || (nth) == 1)
    break ;
  result = result * nth;
  nth = nth - 1
 }
console.log(result); 
return result;
}
发布评论

评论列表(0)

  1. 暂无评论