How to get a JavaScript factorial programs' loop to show the working used?

不羁的心 提交于 2020-12-12 05:38:46

问题


Hello there I have been challenged to write a program in JavaScript despite not really knowing much about it that asks the user for a number and then calculates the factorial of that number. I used already asked questions and managed to get the calculation to work but couldn't get the required output. I have to get it in the following output without using any fancy libraries or extra variables/arrays (which I can't think of how to do) :

(assuming user input is 5):

The factorial of 5 is 5*4*3*2*1=120 
OR
5! is  5*4*3*2*1=120 

Here is the code I've got so far:

//prompts the user for a positive number
var number = parseInt(prompt("Please enter a positive number"));
console.log(number);
//checks the number to see if it is a string
if (isNaN(number)) {
  alert("Invalid. Please Enter valid NUMBER")
}

//checks the number to see if it is negaive
else if (number < 0) {
  alert("Please Enter valid positive number");
}

//if a positive integer is entered a loop is started to calculate the factorial of the number the user entered
else {
  let factorial = 1;
  for (count = 1; count <= number; count++) {
    factorial *= count;
  }

  //Sends the inital number back to the user and tells them the factorial of that number
  alert("The factorial of " + number + " is " + factorial + ".");
}

I know there are many similar questions to this as I looked around and used them to help me get this far but it is getting the output into the required format that I'm struggling with. I am told it is possible with a loop but don't know where to begin implementing that and I'm only allowed to use that solution.

Unfortunately this is part of a larger program in the challenge and I can only use the following variables:

Number (variable initialised as 0 to hold user input) Factorial (variable initialised to 1 to hold value of calculated factorial) Count (variable to hold number of times loop is executed for performing factorial calculation)


回答1:


Probably you just need to build a string in that loop (on top of calculating the actual value):

let input=parseInt(prompt("Number?"));
let output="";
let result=1;
for(let i=input;i>1;i--){
  result*=i;
  output+=i+"*";
}
console.log(input+"! is "+output+"1="+result);

The "no-array clause" in your task presumably means that you are not supposed to build an array and use join() on it, like

let arr=[1,2,3,4,5];
console.log(arr.join("*"));



回答2:


I have updated your code mainly here, Also make sure you are using the same variable num in your code and not number:

let factorials = [];
let result = 1;
for (count = num; count >= 1; count--) {
        result *=count;
        factorials.push(count);
}

//prompts the user for a positive number
var num = parseInt(prompt("Please enter a positive number"));
console.log(num);

//checks the number to see if it is a string
if (isNaN(num))
{
  alert("Invalid. Please Enter valid NUMBER")
}

//checks the number to see if it is negaive
else if (num < 0) 
{
  alert("Please Enter valid positive number");
}

//if a positive integer is entered a loop is started to calculate the factorial of the number the user entered
else {
    let factorials = [];
    let result = 1;
    for (count = num; count >= 1; count--) {
        result *=count;
        factorials.push(count);
         }
    
    //Sends the inital number back to the user and tells them the factorial of that number
    alert("The " + num + "! is " + factorials.join('*') + " is " + result + ".");
}


来源:https://stackoverflow.com/questions/64915157/how-to-get-a-javascript-factorial-programs-loop-to-show-the-working-used

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!