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);
};
}
}
the code output looks like this:
['0']
['1']
['1']
['1']
['1']
['1']
['1']
['1']
['1']
['0']
But I want the output to come out in just an array, like this:
['0111111110']
I've already tried methods like the join() function, but it didn't work:
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());
}
};
Please, if you have any idea how to do this, no matter how, help me if possible.
CodePudding user response:
So it seems like your fundamental misunderstanding is how .push
works. Push, as the name implies, pushes a new value to the end of the array. Thus instead of pushing to an array, what you need to do is just build up a string and then push that string at the end.
Looks like your code will either return a 1 or a 0 depending on what filter. Thus your new code can look like so:
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];
}
Where if we then log it out as so: console.log(calculaNumeroDaSenha(['0110100000', '1001011111','1110001010', '0111010101','0011100110', '1010011001', '1101100100', '1011010100', '1001100111', '1000011000']));
we get an array of size 1 of the following results: ['0111111110']
. JSFiddle: https://jsfiddle.net/avnkj81z/
One thing I did, is I changed your inner function blocks to be defined instead using the following pattern: const <functionName> = <(parameters)> => {...}
. This is because it makes it easier to read and you don't have one function nested in one after the other. Note that function A(b) { returns b; }
is equivalent to const A = (b) => { returns b;}
CodePudding user response:
To join all the strings into one string, use Array.join()
.
CodePudding user response:
The question is kinda vague, you need to clarify what you're looking for:
if you are looking to group an array items into one string, you would use 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"
However if you're looking to group all array elements into one array that has a single string with all array elements grouped, you would use Array.join() followed by 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" ]
CodePudding user response:
I think you want the output of this code. Please look into it. It is printing your desired value.
calculaNumeroDaSenha(['0110100000', '1001011111', '1110001010', '0111010101', '0011100110', '1010011001', '1101100100', '1011010100', '1001100111', '1000011000']);
function calculaNumeroDaSenha(senha) {
var novaArray = [];
var str = '';
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) {
if (x.length > y.length || x.length == y.length) {
str = 1;
}
else {
str = 0;
}
};
}
novaArray.push(str);
console.log(novaArray);
}