Array Generic Annotations
Write a TypeScript function that uses the generic Array<string> syntax. Create a function that joins an array of strings with a separator. The parameter must be typed as Array<string>.
TypeScript supports two syntaxes for typed arrays: string[] and Array<string>. The generic Array<T> syntax is consistent with other generic types like Promise<T> and Map<K, V>.
Using the generic Array syntax makes the type parameter explicit and is preferred in some TypeScript style guides for consistency with other generic types.
The generic Array syntax works seamlessly with other generic patterns. For example, Promise
Example Input & Output
Algorithm Flow
Solution Approach
Write a TypeScript function that uses the generic Array<string> syntax. Create a function that joins an array of strings with a separator. The parameter must be typed as Array<string>.
TypeScript supports two syntaxes for typed arrays: string[] and Array<string>. The generic Array<T> syntax is consistent with other generic types like Promise<T> and Map<K, V>.
Using the generic Array syntax makes the type parameter explicit and is preferred in some TypeScript style guides for consistency with other generic types.
Best Answers
function joinStrings(items: Array<string>, separator: string): string {
if (items.length === 0) return '';
var result = items[0];
for (var i = 1; i < items.length; i++) {
result += separator + items[i];
}
return result;
}Comments (0)
Join the Discussion
Share your thoughts, ask questions, or help others with this Challenge.
