calculaNumeroDaSenha(['0110100000', '1001011111','1110001010', '0111010101','0011100110', '1010011001', '1101100100', '1011010100', '1001100111', '1000011000']);
function calculaNumeroDaSenha(senha) {
for (let i = 0; i < senha.length; i ) {
var dividir = senha[i].split('');
function filtro1(modo1) {
return modo1 == "1"
}
function filtro2(modo2) {
return modo2 == "0"
}
etapaFinal(dividir.filter(filtro1), dividir.filter(filtro2));
function etapaFinal(x,y) {
var novaArray = [];
if (x.length > y.length || x.length == y.length) {
novaArray.push('1')}
else {
novaArray.push('0');
}
console.log(novaArray);
};
}
}
代碼輸出如下所示:
['0']
['1']
['1']
['1']
['1']
['1']
['1']
['1']
['1']
['0']
但我希望輸出只出現在一個陣列中,如下所示:
['0111111110']
我已經嘗試過 join() 函式之類的方法,但它不起作用:
function etapaFinal(x,y) {
var novaArray = [];
if (x.length > y.length || x.length == y.length) {
novaArray.push('1')}
else {
novaArray.push('0');
}
for (let a = 0; a < novaArray.length; a ) {
console.log(novaArray[i].join());
}
};
請,如果你有任何想法如何做到這一點,無論如何,如果可能的話,請幫助我。
uj5u.com熱心網友回復:
因此,您的基本誤解似乎是如何.push作業。Push,顧名思義,將一個新值推送到陣列的末尾。因此,無需推送到陣列,您需要做的只是構建一個字串,然后在最后推送該字串。
看起來您的代碼將回傳 1 或 0,具體取決于過濾器。因此,您的新代碼可能如下所示:
function calculaNumeroDaSenha(senha) {
let result = '';
for (let i = 0; i < senha.length; i ) {
var dividir = senha[i].split('');
const filtro1 = (modo1) => {
return modo1 == "1"
}
const filtro2 = (modo2) => {
return modo2 == "0"
}
const etapaFinal = (x,y) => {
if (x.length > y.length || x.length == y.length) {
return '1';
}
else {
return '0';
}
};
result = etapaFinal(dividir.filter(filtro1), dividir.filter(filtro2));
}
return [result];
}
如果我們這樣注銷它: console.log(calculaNumeroDaSenha(['0110100000', '1001011111','1110001010', '0111010101','0011100110', '1010011001', '1101100100', '1011010100', '1001100111', '1000011000']));
我們得到一個大小為 1 的陣列,結果如下['0111111110']:JSFiddle:https ://jsfiddle.net/avnkj81z/
我做的一件事是我改變了你的內部功能塊,而不是使用以下模式來定義:const <functionName> = <(parameters)> => {...}. 這是因為它使閱讀更容易,并且您沒有一個函式嵌套在另一個函式中。請注意,這function A(b) { returns b; }相當于const A = (b) => { returns b;}
uj5u.com熱心網友回復:
要將所有字串連接成一個字串,請使用Array.join().
uj5u.com熱心網友回復:
這個問題有點模糊,你需要澄清你在找什么:
如果您希望將一個陣列項組合成一個字串,您可以使用 Array.join()。
const arr = ['0110100000', '1001011111','1110001010', '0111010101','0011100110', '1010011001', '1101100100', '1011010100', '1001100111', '1000011000']
arr.join('');
// the above command would result in a single string with all array values concatenated
// "0110100000100101111111100010100111010101001110011010100110011101100100101101010010011001111000011000"
但是,如果您希望將所有陣列元素分組到一個陣列中,該陣列有一個字串,所有陣列元素都分組,您將使用 Array.join() 后跟 String.split()
const arr = ['0110100000', '1001011111','1110001010', '0111010101','0011100110', '1010011001', '1101100100', '1011010100', '1001100111', '1000011000']
arr.join('').split();
// the above command would result in a single array with one string with all array elements grouped
// [ "0110100000100101111111100010100111010101001110011010100110011101100100101101010010011001111000011000" ]
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/525734.html
上一篇:查找特定范圍內的素數
下一篇:如何比較串列中的最小值?
