Return the number of characters in a word list with Typescript

Asked

Viewed 95 times

-1

I want to return a method that receives a list of words and returns an array with the number of characters of the words. I tried as below, but is returning the amount of words instead of characters.

   //Cria lista de palavras

    var caracteres:string[] = new Array( "Celular", "Carro", "Pessoa" );

    function quantidadeCaracteres( a, b )
        return a + b;
    }
    console.log( caracteres.length );

2 answers

-1

Well you can use the existing map method in ES6 I advise you to read more about the API.

But the idea is to precorrer all array and each time precorreres detects the size of each word and at the end have a variable that contains the total of each word.

var caracteres: string[] = new Array("Celular", "Carro", "Pessoa"); // Teu Array
let total: number[] = caracteres.map((caractere) => caractere.length); // Metodo que mapeada cada registro e retorna o total
console.log(total); //Exubicao do total

-4


//Cria lista de palavras

    var caracteres:string[] = new Array("Celular","Carro","Pessoa");
    var total = caracteres.toString();

    function quantidadeCaracteres( a, b ){
        return a + b;
    }
    console.log( total.length );

  • In this case sum 1 more character in each word, would be due to the use of the comma?

  • This by the comma.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.