MST

星途 面试题库

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

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

知识考点

AI 面试

面试题答案

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