简体   繁体   English

如何获得 JavaScript 阶乘程序的循环以显示使用的工作?

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

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.你好,我一直面临着用 JavaScript 编写程序的挑战,尽管我对它的了解并不多,该程序要求用户输入一个数字,然后计算该数字的阶乘。 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): (假设用户输入为 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) Number(变量初始化为 0 以保存用户输入) Factorial(变量初始化为 1 以保存计算阶乘的值) Count(变量以保存执行阶乘计算的循环次数)

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您的任务中的“无数组子句”可能意味着您不应该构建数组并在其上使用join() ,例如

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

I have updated your code mainly here, Also make sure you are using the same variable num in your code and not number :我主要在这里更新了您的代码,还要确保您在代码中使用相同的变量num而不是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 + "."); }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM