Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

JS – Count characters of each array words

Im a very beginner.

I need to write down for an array list the array and then the number of characters that array concists of.

example

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

apple 5
banana 6
grapefruit 10

I have trouble to count the characters of one array.
I only know to count the list of arrays, ie. in this example 3.

It needs to be a loop.
So for I have this

const fruit = ["apple", "banana", "grapefruit"];

let fruitCountChar = 0;    
   for (let i = 0; i < fruit.length; i++) {    
      fruitCountChar = fruit.length;
      console.log(fruit[i], fruitCountChar);
} 

>Solution :

You already managed to successfully log the fruit names to console, all you need to do now, is access the length property of the exact same thing:

const fruit = ["apple", "banana", "grapefruit"];

let fruitCountChar = 0;    
   for (let i = 0; i < fruit.length; i++) {    
      fruitCountChar = fruit[i].length;
                         // ^^^
      console.log(fruit[i], fruitCountChar);
} 
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading