简体   繁体   English

更新帖子:如何获得 JavaScript 阶乘程序的循环以显示使用的工作? 查看要求

[英]Updated post: How do I get a JavaScript factorial programs' loop to show the working used? See requirements

this is my second post today as the original wasn't clear and I was urged to repost because despite getting some good answers they did not fit the requirements of the code.这是我今天的第二篇文章,因为原文不清楚,我被敦促重新发布,因为尽管得到了一些很好的答案,但它们不符合代码的要求。 I have been challenged to write a program in JavaScript that allows the user to perform several tasks, one of which is to ask the user for a number and calculate the factorial of that number and then display it in the format listed in the requirements.我一直面临着用 JavaScript 编写允许用户执行多项任务的挑战,其中一项任务是向用户询问一个数字并计算该数字的阶乘,然后以需求中列出的格式显示它。 As I do not know much about Java script I used already asked questions and managed to get the calculation to work but could not figure out how to get the required output whilst still meeting the requirements.由于我对 Java 脚本知之甚少,因此我使用了已经提出的问题并设法使计算工作,但无法弄清楚如何在满足要求的同时获得所需的输出。

Requirements: • Can only use the provided 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(变量保存执行阶乘计算的循环次数)。 This is a limitation set by the challenge and not me • Cannot use fancy libraries • Need to use a loop solution for the output.这是挑战而不是我设置的限制 • 不能使用花哨的库 • 需要为输出使用循环解决方案。 The answers on the other post required introducing new variables, perhaps it is my lack of understanding but perhaps the poorly written pseudo code I have obtained since the last post may help.另一篇文章的答案需要引入新变量,也许是我缺乏理解,但也许自上一篇文章以来我获得的写得不好的伪代码可能会有所帮助。 • Be output in the format: (it is an alert so that part of the program is fine) • 以下列格式输出:(这是一个警告,以便程序的一部分正常)

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

Poorly written pseudo code:写得不好的伪代码:

在此处输入图片说明

Code:代码:

//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, including my first post which this one now succeeds 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 do not know where to begin implementing that and I'm only allowed to use that solution.我被告知可以使用循环,但不知道从哪里开始实施,我只能使用该解决方案。

Again, I would like to apologise for my first post, the given answers would work great if not for the incredibly ridiculous restrictions set by the challenge provider, who is also responsible for giving me rubbish pseudo code, which isn't what I'm going for but I am using to consider the loop.再次,我想为我的第一篇文章道歉,如果不是挑战提供者设置的令人难以置信的荒谬限制,给出的答案会很好用,他也负责给我垃圾伪代码,这不是我的意思继续前进,但我正在考虑循环。

I appreciate the time it takes to read this amd provide solutions so I will go back and try and mark all working answers in the last post for any normal problems people might search for answers for.我很感激阅读这篇文章所花费的时间并提供解决方案,所以我会回去尝试在最后一篇文章中标记所有有效的答案,以解决人们可能会寻找答案的任何正常问题。

This is a bit of a dirty hack but it should satisfy the condition that no other variables than number , count , and factorial are used.这有点脏,但它应该满足不使用除numbercountfactorial之外的其他变量的条件。

 let number = 5; let factorial = 120; // ^ Do your own calculation for this alert(`The factorial of ${number} is ${Array.from(Array(number + 1).keys()).slice(1).reverse().join("*")}=${factorial}`)

So what is going on here?那么这里发生了什么? We use an interpolated template string to produce the desired output, expressions inside ${these things} are evaluated as strings.我们使用内插模板字符串来生成所需的输出, ${these things}中的表达式被评估为字符串。 And what is the mess we put in there?我们在那里放了什么乱七八糟的东西?

Array.from(Array(number + 1).keys())

The expression above creates the array [0,1,2,3,4,5] .上面的表达式创建了数组[0,1,2,3,4,5]

.slice(1) gives us [1,2,3,4,5] .slice(1)给我们[1,2,3,4,5]

.reverse() gives us [5,4,3,2,1] .reverse()给我们[5,4,3,2,1]

join("*") gives us "5*4*3*2*1" join("*")给我们"5*4*3*2*1"

Which when all put together gives us The factorial of 5 is 5*4*3*2*1=120当所有这些放在一起给我们The factorial of 5 is 5*4*3*2*1=120

And voila!瞧! The output is printed in the desired format without introducing any new variables.输出以所需的格式打印,而不会引入任何新变量。

Edit: Oh and you do not need to use an interpolated string for this.编辑:哦,您不需要为此使用内插字符串。 You may as well concatenate regular strings together as you have done in your question.您也可以像在问题中所做的那样将常规字符串连接在一起。 eg例如

 "The factorial of " + factorial + " is " + Array.from(Array(number + 1).keys()).slice(1).reverse().join("*") + "=" + factorial

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

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