[英]Product, Sum, and Digits of a Number
My aim is to create a program that finds: 我的目的是创建一个查找以下内容的程序:
I've attempted to complete the first 2 parts, as shown in the codepen link: https://codepen.io/anon/pen/LdjveQ . 我尝试完成前两部分,如codepen链接所示: https ://codepen.io/anon/pen/LdjveQ。 Javascript shown below: 如下所示的Javascript:
function result() {
var take = document.getElementById("number").value;
return eval(take.replace(/(\d)(?=\d)/g, '$1+'));
}
function answer() {
document.getElementById('resultfinal').innerHTML = result();
}
function result2() {
var sake = document.getElementById("integers").value;
var mult = (sake*(sake+1));
return eval(mult/2);
}
function answer2() {
document.getElementById('sumofn').innerHTML = result2();
}
I have no idea: 我不知道:
If a "brute force" approach is doable, I would first define P(n)
as a function (se p
function below) and sum it up using a for
: 如果可以使用“蛮力”方法,我首先将P(n)
定义为一个函数(下面的se p
函数),然后使用for
对其求和:
function p(inputN) { var n = inputN, m = 1, digit; while (n) { digit = n % 10; if (digit) m *= digit; n = Math.floor(n / 10); } return m; } function result3() { var sum = 0, i, n = document.getElementById("tpn-input").value; for(var i = 1; i <= n; i++) { sum += p(i); } return sum; } function answer3() { document.getElementById('tpn').innerHTML = result3(); }
<input type="text" id="tpn-input"><button onClick="answer3()">P(n)</button> <p id = "tpn"></p>
There are other somewhat more readable approaches using string .split()
, .filter()
together with .reduce()
, but the above will certainly be more performatic. 存在使用串其他稍微更可读的方法.split()
.filter()
连同.reduce()
但上述肯定会更performatic。
And, just for the heck of it, an over-oneliner short implementation: 而且,仅此而已,它是一个过于单一的简短实现:
function p(n) { return n.toString().split('').map(Number).filter(i => i).reduce((a, b) => a * b, 1); } function tp(n) { return [...Array(+n).keys()].map(i => p(i+1)).reduce((a, b) => a + b, 0); } function answer3() { document.getElementById('tpn').innerHTML = tp(document.getElementById("tpn-input").value); }
<input type="text" id="tpn-input"><button onClick="answer3()">P(n)</button> <p id = "tpn"></p>
You should start by trying using some of the array methods to iterate over an array from 1
to n
. 您应该首先尝试使用一些数组方法来迭代从1
到n
的数组。 reduce
would be the most appropriate for this situation: it takes in an array and returns some cumulative value from an operation (or few) done over each element in the array. reduce
将最适合这种情况:它接收一个数组,并从对该数组中的每个元素执行的一个操作(或几个操作)返回一些累加值。 For example: 例如:
function sumUpToN(n) { return Array.from({ length: n + 1}, (_, i) => i) .reduce((partialSum, thisNum) => partialSum + thisNum); } console.log(sumUpToN(5));
Generate your arrays using Array.from like that, and then iterate over them with .reduce
. 使用Array.from这样生成的数组,然后遍历他们.reduce
。 The first argument is the returned value from the previous iteration of .reduce
, and the second is the current element in the array. 第一个参数是.reduce
上一次迭代的返回值,第二个参数是数组中的当前元素。 Try using a similar method when you need to iterate over the whole array and come out with a single value on the other end - except instead of plain addition (which will produce the sum of all the elements in the array), use whatever operator(s) are appropriate for the problem in question. 当您需要遍历整个数组并在另一端给出一个单一值时,请尝试使用类似的方法-除了代替普通加法(它将产生数组中所有元素的和)外,请使用任何运算符( s)适用于所讨论的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.