在计算数字的阶乘时,我如何写出每个单独的数字以及星号?
How could i write out each individual number as well as the asterisk symbol when calculating the factorial of a number?
我遇到了一个问题,需要我显示阶乘函数的完整工作原理,例如,如果用户想锻炼 6!,我需要显示:6 * 5 * 4 * 3 * 2 * 1 = 720。我需要为此使用数组吗?
到目前为止,这是我用来计算任何用户给定数字的因式分解值的方法,尽管这只输出最终值,而不是我上面显示的完全扩展的计算结果:
(变量号包含用户输入);
var f = [];
function factorizeFunction(number) { //this is the function that does the factorization calculations
if (number == 0 || number == 1)
return 1;
if (f[number] > 0)
return f[number];
return f[number] = factorizeFunction(number-1) * number;
}
document.getElementById("factorialTest").innerHTML = factorizeFunction(number);
如有任何帮助,我们将不胜感激!
使用map
和join
方法。
const factorString = num => {
const nums = new Array(num).fill(0).map((_, i) => num - i);
let res = 1;
nums.forEach(x => res *= x);
return `${nums.join(' * ')} = ${res}`;
}
console.log(factorString(6))
一个选项是,在每次迭代时,推送到一个数组,该数组通过递归调用向下传递(或在初始调用时创建)。最后,return 由 *
连接的数组,以及数组的总和:
function factorizeFunction(number, arr = []) { //this is the function that does the factorization calculations
if (number == 0 || number == 1) arr.push(number);
else {
arr.push(number);
factorizeFunction(number - 1, arr);
}
return arr.join(' * ') + ' = ' + arr.reduce((a, b) => a * b, 1);
}
document.getElementById("factorialTest").innerHTML = factorizeFunction(5);
<div id="factorialTest"></div>
您可以更改函数的 return 签名,并期望一个包含因子和乘积的数组的数组。
function factorize(number) {
if (number === 0 || number === 1) return [[1], 1];
var [factors, product] = factorize(number - 1);
return [[...factors, number], product * number];
}
console.log(factorize(5));
我遇到了一个问题,需要我显示阶乘函数的完整工作原理,例如,如果用户想锻炼 6!,我需要显示:6 * 5 * 4 * 3 * 2 * 1 = 720。我需要为此使用数组吗?
到目前为止,这是我用来计算任何用户给定数字的因式分解值的方法,尽管这只输出最终值,而不是我上面显示的完全扩展的计算结果:
(变量号包含用户输入);
var f = [];
function factorizeFunction(number) { //this is the function that does the factorization calculations
if (number == 0 || number == 1)
return 1;
if (f[number] > 0)
return f[number];
return f[number] = factorizeFunction(number-1) * number;
}
document.getElementById("factorialTest").innerHTML = factorizeFunction(number);
如有任何帮助,我们将不胜感激!
使用map
和join
方法。
const factorString = num => {
const nums = new Array(num).fill(0).map((_, i) => num - i);
let res = 1;
nums.forEach(x => res *= x);
return `${nums.join(' * ')} = ${res}`;
}
console.log(factorString(6))
一个选项是,在每次迭代时,推送到一个数组,该数组通过递归调用向下传递(或在初始调用时创建)。最后,return 由 *
连接的数组,以及数组的总和:
function factorizeFunction(number, arr = []) { //this is the function that does the factorization calculations
if (number == 0 || number == 1) arr.push(number);
else {
arr.push(number);
factorizeFunction(number - 1, arr);
}
return arr.join(' * ') + ' = ' + arr.reduce((a, b) => a * b, 1);
}
document.getElementById("factorialTest").innerHTML = factorizeFunction(5);
<div id="factorialTest"></div>
您可以更改函数的 return 签名,并期望一个包含因子和乘积的数组的数组。
function factorize(number) {
if (number === 0 || number === 1) return [[1], 1];
var [factors, product] = factorize(number - 1);
return [[...factors, number], product * number];
}
console.log(factorize(5));