Imagem de fundo

Um programador teve por tarefa escrever uma função...

Um programador teve por tarefa escrever uma função Javascript que receba uma string como parâmetro e retorne outra string. A string retornada deve conter os mesmos caracteres que a string recebida, sem espaços em branco.


Exemplo:

String recebida: ‘ B r a s il ‘ (total de 14 caracteres e 8 espaços em branco)

String retornada: ‘Brasil’ (total de 6 caracteres e nenhum espaço em branco)


Considere que, na função a ser escrita, duas aspas simples seguidas (‘‘) representam uma string vazia, e duas aspas simples com um espaço entre elas (‘ ‘) representam um caracter de espaço em branco.


Qual função realiza o que foi pedido a esse programador?


A

function tiraBranco(str) {

-----var i=0,len=str.length,aux=’’;

--------while(i<len) {

----------if(str[i]===’ ‘)

---------------aux=aux+str.slice(i+1,str.length);

---------------i++;

----------}

----------return aux;

}


B

function tiraBranco(str) {

-----if(str.length===0)

----------return ‘’;

-----if(str[0]===’ ‘)

----------return tiraBranco(str.slice(1,str.length));

else

-----return str[0]+tiraBranco(str.slice(1,str.length));

}


C

function tiraBranco(str) {

-----var i=0,len=str.length,aux=’’;

-------while(i<len) {

------------if(str[i]===’ ‘)

----------------aux=aux+str[i];

------------i++;

}

--------return aux;

}


D

function tiraBranco(str) {

---var aux=’’;

---for(let c in str) {

------if(c!==’ ‘)

------aux+=c;

}

---return aux;

}


E

function tiraBranco(str) {

---if(str.length===0)

------return ‘’;

---var c=str[0],

---ret; ret=tiraBranco(str.slice(str.length));

---if(str[0]===’ ‘)

------return ret;

---else

------return c+ret;

}