MST

星途 面试题库

面试题:TypeScript中可选参数与默认参数的基础应用

请编写一个TypeScript函数,该函数接受两个参数,第一个参数为必选的字符串类型,第二个参数为可选的数字类型且默认值为10。函数的功能是返回字符串重复指定次数后的结果。例如,传入('hello', 3)应返回'hellohellohello',若只传入('hello'),应返回'hello'重复10次的结果。
34.3万 热度难度
前端开发TypeScript

知识考点

AI 面试

面试题答案

一键面试
function repeatString(str: string, times: number = 10): string {
    let result = '';
    for (let i = 0; i < times; i++) {
        result += str;
    }
    return result;
}