
原文标题:《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.观念,概念;观点;思想,设想,想法;总的印象

Algorithm Challenge

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);

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

公众号
前端达人
长按识别左边二维码关注我
点击下方“阅读原文”查看英文原文↓↓↓