1
I’m learning development Web with HTML, CSS and Javascript and I came across the problem of generating a phone number with parentheses, dash and space. As I’m starting I developed a code that returns me good results, but does not pass the level of complexity of Codeclimate. Can anyone help me? I am very grateful for the help?
function generatePhoneNumber(vetor) {
let countRepeat = vetor.filter((e, i, a) => a.indexOf(e) !== i);
countRepeat.sort(function (a, b) { return a - b; });
let contagemReal = 0;
let confere = 0;
for (let i = 0; i < countRepeat.length; i += 1) {
if (countRepeat[i] === countRepeat[i + 1] || countRepeat[i] ===
countRepeat[i + 2]) {
contagemReal = 3;
}
}
for (let i in vetor) {
if (vetor[i] > 9 || vetor[i] < 0) {
confere += 1;
}
}
if (vetor.length !== 11) {
return 'Array com tamanho incorreto.';
} else if (contagemReal >= 3 || confere !== 0) {
return 'não é possível gerar um número de telefone com esses valores';
}
vetor.splice(0, 0, '(');
vetor.splice(3, 0, ')');
vetor.splice(4, 0, ' ');
vetor.splice(10, 0, '-');
return vetor.join('');
}