前端面试题收藏集 ---- 一

将一个字符串翻转,例 '123456' -> '654321'

// 翻转字符串算法
function fanzhuan( str ) {
    let result = ''
    for ( let i = str.length; i > 0; i-- ) {
        result += str.slice( i-1, i )
    }
    return result
}
let str = '123456'
let str2 = fanzhuan( str )      // 654321
let str3 = fanzhuan( '123456789abc' )   //cba987654321
let str4 = fanzhuan( '清欢' )     // 欢清

有a,b,c...等十个js文件,需要按照顺序引入,使用promise封装

// 封装 promise
function promiseOnloadJS ( url ) {
    return new Promise( ( resolve, reject )=> {
        const node = document.createElement('script')
        node.type = 'text/javascript'
        node.src = url
        node.onload = function() {  // 在js 文件加载完成 ,调用resolve
            resolve()
        }
        document.head.appendChild(node)
    })
}
promiseOnloadJS('js/temp/a.js').then(function() {
    console.log('---------------');
    return promiseOnloadJS('js/temp/b.js')
}).then(function() {
    console.log('---------------');
    return promiseOnloadJS('js/temp/c.js')
}).then(function() {
    console.log('---------------');
    return promiseOnloadJS('js/temp/d.js')
}).then(function() {
    console.log('---------------');
    return promiseOnloadJS('js/temp/e.js')
}).then(function() {
    console.log('---------------');
    return promiseOnloadJS('js/temp/f.js')
}).then(function() {
    console.log('---------------');
    return promiseOnloadJS('js/temp/g.js')
}).then(function() {
    console.log('---------------');
    return promiseOnloadJS('js/temp/h.js')
}).then(function() {
    console.log('---------------');
    return promiseOnloadJS('js/temp/i.js')
})
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容