Currently delving into TypeScript, I have set myself the task of crafting a function that takes in a string parameter and reverses each word within the string.
Here is what I aim to achieve with my output:
"This is an example!" ==> "sihT si na !elpmaxe"
The solution I've concocted involves steering clear of built-in methods.
export function reverseWords(str: string): string {
var newStr = "";
str.split("");
for(var i = str.length -1; i >= 0; i--){
newStr += str[i];
}
return newStr;
}
reverseWords("Hi. How are you?")
However, this code yields the following result:
"This is an example!" ==> !elpmaxe na si sihT
What I am actually striving for is:
"This is an example!" ==> "sihT si na !elpmaxe"
If anyone could shed some light on where I may be going awry, it would be greatly appreciated.