[英]“System cannot find the file specified” when trying to run PowerShell script
[英]How to fix: The system cannot find the file specified - .reduce() in JS relevant
編寫一個接受正參數num並返回其乘性持久性的函數,這是您必須將num中的數字相乘直到達到一位的次數。
例如:939-> 9 * 3 * 9 = 243-> 2 * 4 * 3 = 24-> 2 * 4 = 8 //總計:3次
我嘗試了代碼,但返回了:
在Atom上:“系統找不到指定的文件。” //我使用腳本包
在repl.it上:“ tempoOne.reduce()不是函數”
`
let persistence = (num) => {
let tempoOne, tempoTwo;
let count = 0;
let changeToString = num.toString();
let numArray = changeToString.split('');
var calculator = (accumulator, currentValue) => accumulator*currentValue;
tempoOne = numArray;
do {
tempoOne = tempoOne.reduce(calculator);
count++;
} while (tempoOne/10 >= 0);
return count;
}
console.log(persistence(939));`
將您的tempoOne
返回string
並在迭代中split
:
let persistence = (num) => { let tempoOne, tempoTwo; let count = 0 var calculator = (accumulator, currentValue) => accumulator*parseInt(currentValue); tempoOne = num; do { tempoOne = tempoOne.toString().split('').reduce(calculator, 1); count++; } while (tempoOne/10 > 1); return count; } console.log(persistence(939));
我注意到您的方法中有幾個錯誤會導致一些錯誤:
split()
,您將得到一個字符串數組,因此您必須在將其相乘之前將其解析為int,因為您不想對字符串進行乘法(將parseInt
添加到了reduce callback
並以中性乘法開始因子: 1
)。 split
到循環外,因為您希望tempoOne
成為循環內的字符串, split()
可以正常工作(在循環外刪除了split
並添加到循環中)。 您可能要再次拆分reduce
的結果:
let persistence = (num) => { let tempoOne, tempoTwo; let count = 0; let numArray = num.toString().split(''); var calculator = (accumulator, currentValue) => accumulator * currentValue; var result; tempoOne = numArray; do { result = tempoOne.reduce(calculator); tempoOne = result.toString().split(''); count++; } while (result / 10 > 1); return count; } console.log(persistence(939));
沒有result
變量:
let persistence = (num) => { let tempoOne, tempoTwo; let count = 0; let numArray = num.toString().split(''); var calculator = (accumulator, currentValue) => accumulator * currentValue; var result; tempoOne = numArray; do { tempoOne = tempoOne.reduce(calculator).toString().split(''); count++; } while (tempoOne.length > 1); return count; } console.log(persistence(939));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.