一起学英语 | 用JavaScript实现数字阶乘的三种方法

本文介绍了如何在JavaScript中使用递归、while循环和for循环三种方式实现数字阶乘。文章详细讲解了每种方法的实现原理,并提供了相应的代码示例。通过学习,可以加深对JavaScript算法和阶乘概念的理解。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

0?wx_fmt=png点击上方“前端达人” 可以订阅哦!

原文标题:《Three Ways to Factorialize a Number in JavaScript

原文作者:Sonya Moisset

原文链接:https://medium.freecodecamp.com/how-to-factorialize-a-number-in-javascript-9263c89a4b38

翻译参考:柯林斯词典 谷歌翻译 百度翻译

前端技术日新月异,好多新的技术文章都是英文的,这就要求我们有着良好的英文能力,从今天开始小编和大家一起边学英文边学前端,由于能力有限,欢迎大家指正,一起共同提高英文。

This article is based on Free Code Camp Basic Algorithm Scripting “Factorialize a Number”

译:

本篇文章基于Free Code Camp网站的基础算法脚本"数字阶乘"。

注:Free Code Camp,一个在线免费学习网站开发的网站。网站地址:https://www.freecodecamp.com

重点词汇:

algorithm 

n.(尤指电脑程序中的)算法,运算法则

In mathematics, the factorial of a non-negative integer n can be a tricky algorithm. In this article, I’m going to explain three approaches, first with the recursive function, second using a while loop and third using a for loop.

译:

在数学中,非负整数n的阶乘可能是一个棘手的算法。 在本文中,我将通过三种方法实现,首先是递归函数,第二种使用while循环,第三种使用for循环。

重点词汇:

factorial

adj.因子的,阶乘的

n.阶乘积,阶乘

non-negative

非负

tricky

adj. 难办的;难对付的;棘手的;诡计多端的;奸诈的;狡猾的。

approach

vt.& vi.接近,走近,靠近

vt.接近;着手处理;使移近;试图贿赂(或影响,疏通)

n.方法;途径;接近

vi.靠近

recursive function

递归函数

We have already seen a recursion approach on a String in the previous article, How to Reverse a String in JavaScript in 3 Different Ways ? This time we will apply the same concept on a number.

译:

我们已经在上篇文章看过递归方法在字符串处理的运用,如何用三种方法在JavaScript实现反转字符串?这次我们将应用相同的概念来处理数字。

重点词汇:

concept

n.观念,概念;观点;思想,设想,想法;总的印象

0?wx_fmt=png

Algorithm Challenge

0?wx_fmt=png

Return the factorial of the provided integer.


If the integer is represented with the letter n, a factorial is the product of all positive integers less than or equal to n.


Factorials are often represented with the shorthand notation n!


For example: 5! = 1 * 2 * 3 * 4 * 5 = 120

译:

挑战算法

返回一个指定整数的阶乘。

如果整数用字母N表示,阶乘是小于或等于n的所有正整数的乘积。

阶乘通常以速记符号N!表示。

例如:5!= 1 * 2 * 3 * 4 * 5 = 120

function factorialize(num) {    

return num;    

}    

factorialize(5);    

重点词汇:

represented

v.代表;体现;表现( represent的过去式和过去分词 );作为…的代表

positive

adj.

积极的;确实的,肯定的;[数]正的;[医]阳性的

n.

正面;正片;[语]原级形容词;[数]正量

shorthand notation

简化符号

Provided test cases

  • factorialize(0) should return 1

  • factorialize(5) should return 120

  • factorialize(10) should return 3628800

  • factorialize(20) should return 2432902008176640000

译:

提供测试用例:

  • factorialize(0) 应该返回1

  • factorialize(5) 应该返回120

  • factorialize(10) 应该返回3628800

  • factorialize(20)应该返回2432902008176640000

重点词汇:

provided

conj.如果;假如;在…的条件下

v.提供,供给( provide的过去式和过去分词)

What is factorializing a number all about?


When you factorialize a number, you are multiplying that number by each consecutive number minus one.


If your number is 5, you would have:

5! = 5 * 4 * 3 * 2 * 1

The pattern would be:

0! = 1
1! = 1
2! = 2 * 1
3! = 3 * 2 * 1
4! = 4 * 3 * 2 * 1
5! = 5 * 4 * 3 * 2 * 1

译:

到底什么是数字阶乘?

当你计算数字阶乘,每次将数字递减1,并相乘。

如果数字是5,你应该这样:

5! = 5 * 4 * 3 * 2 * 1

该模式将会:

0! = 1
1! = 1
2! = 2 * 1
3! = 3 * 2 * 1
4! = 4 * 3 * 2 * 1
5! = 5 * 4 * 3 * 2 * 1

重点词汇:

consecutive

adj.连续的,连贯的;[语]表示结果的

multiplying

v.乘( multiply的现在分词 );(使)相乘;(使)增加;(使)繁殖

1. Factorialize a Number With Recursion

function factorialize(num) {    

if (num < 0)    

return -1;    

else if (num == 0)    

return 1;    

else {    

return (num * factorialize(num - 1));    

}    

}    

factorialize(5);    

2. Factorialize a Number with a WHILE loop

function factorialize(num) {    

var result = num;    

if (num === 0 || num === 1)    

return 1;    

while (num > 1) {    

num--;    

result *= num;    

}    

return result;    

}    

factorialize(5);    

3. Factorialize a Number with a FOR loop

function factorialize(num) {    

if (num === 0 || num === 1)    

return 1;    

for (var i = num - 1; i >= 1; i--) {    

num *= i;    

}    

return num;    

}    

factorialize(5);    

0?wx_fmt=png

关于今天的文章分享就到这里,希望大家有所收获,关于英文原文请点击阅读原文查看(需要翻墙才能看到,你懂得)

0?wx_fmt=jpeg

公众号

前端达人

长按识别左边二维码关注我

点击下方“阅读原文”查看英文原文↓↓↓ 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值