0

Typescrip counting words in string HELP :)

I need to count words in string, if there are 2 words i need to get the first letters from those two words and if there is one word i needbto get the first two letters from the word.

11th Mar 2021, 12:41 PM
Kate Atanasoska
Kate Atanasoska - avatar
4 Antworten
+ 1
Try to split the problem into small pieces so you can solve it . 1- first split the string ( use the split method) by space , so it will returns an array of words . 2- iterate through that array and return the first letter Hope this Helps
11th Mar 2021, 2:05 PM
Med Amine Fh
Med Amine Fh - avatar
+ 1
Have you solved this Kate Atanasoska ? Similar to what Med said, a good first step is to split the string by a space which returns an array of a word, or words. You can then get the number of words by getting the length of the array. Then, you can use some conditional logic like an if statement: if one word...else... You can use an index number in square brackets to take a word out of the array. Then on the word string, you can either use an index number in square brackets again to get an individual letter or use a string method like slice().
12th Mar 2021, 11:20 PM
CamelBeatsSnake
CamelBeatsSnake - avatar
+ 1
Thank you, i did it.
13th Mar 2021, 12:48 AM
Kate Atanasoska
Kate Atanasoska - avatar
+ 1
Nice 😊 well done
13th Mar 2021, 12:53 AM
CamelBeatsSnake
CamelBeatsSnake - avatar