admin管理员组文章数量:1321829
I've seen other answers but I'm trying to make my own with a callback for pratice.
"Using the JavaScript language, have the function MultiplicativePersistence(num) take the num parameter being passed which will always be a positive integer and return its multiplicative persistence which is the number of times you must multiply the digits in num until you reach a single digit. For example: if num is 39 then your program should return 3 because 3 * 9 = 27 then 2 * 7 = 14 and finally 1 * 4 = 4 and you stop at 4. "
function MultiplicativePersistence(num) {
function multiply(n){
n.reduce(function(a,b){return a*b;});
}
var count =0;
while(num.toString().length > 1) {
num= num.split("");
num = multiply(num);
count++;
return count;
}
}
MultiplicativePersistence("39"); // Should return 3 but it's returning 1
I've seen other answers but I'm trying to make my own with a callback for pratice.
"Using the JavaScript language, have the function MultiplicativePersistence(num) take the num parameter being passed which will always be a positive integer and return its multiplicative persistence which is the number of times you must multiply the digits in num until you reach a single digit. For example: if num is 39 then your program should return 3 because 3 * 9 = 27 then 2 * 7 = 14 and finally 1 * 4 = 4 and you stop at 4. "
function MultiplicativePersistence(num) {
function multiply(n){
n.reduce(function(a,b){return a*b;});
}
var count =0;
while(num.toString().length > 1) {
num= num.split("");
num = multiply(num);
count++;
return count;
}
}
MultiplicativePersistence("39"); // Should return 3 but it's returning 1
Share
Improve this question
edited Dec 22, 2014 at 15:53
Sampath Liyanage
4,8962 gold badges31 silver badges41 bronze badges
asked Dec 22, 2014 at 15:11
Alexandra ChenAlexandra Chen
211 silver badge3 bronze badges
1
-
You forgot the
return
inmultiply
. – Bergi Commented Dec 22, 2014 at 15:21
4 Answers
Reset to default 4There were 3 mistakes.
- The return should not be inside the loop.
num
should be converted to string before splitting.multiply
function should return the results
function MultiplicativePersistence(num) {
function multiply(n){
return n.reduce(function(a,b){return a*b;});
}
var count =0;
while(num.toString().length > 1) {
num= num.toString().split("");
num = multiply(num);
count++;
}
return count;
}
alert(MultiplicativePersistence("39"));
You can use the following solution to solve your problem:
function persistenceB(num) {
var times = 0;
num = num.toString();
while (num.length > 1) {
times++;
num = num.split('').map(Number).reduce((a, b) => a * b).toString();
}
return times;
}
function persistence(num) {
if(num.toString().length > 1){
var mult = 1;
var splitStr = num.toString().split('');
for(var i = 0; i<splitStr.length; i++) {
mult *= parseInt(splitStr[i]);
}
return 1 + persistence(mult);
} else{
return 0;
}
const persistence = num => {
return `${num}`.length > 1
? 1 + persistence(`${num}`.split('').reduce((a, b) => a * +b))
: 0;
}
本文标签: callbackMultiplicative Persistence CoderByte JavaScriptStack Overflow
版权声明:本文标题:callback - Multiplicative Persistence CoderByte JavaScript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742103711a2420921.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论