以前的一篇主要是 jQuery 和网络模型的知识点,这一篇则是侧重于编程实现,也是我二面所问的一些内容。
function A() { this.name = "a"; this.getName = function() { return this.name; }; } var a = new A();
/** * 编程实现new操做符 */ var a = {}; a.__proto__ = A.prototype; A.call(a); console.log(a.name); //a
function template(tmpl, data) { // TODO } template("个人名字是(name),个人工做是(work)", { name: "xxx", work: "yy" }); // 函数的输出是 '个人名字是xxx,个人工做是yy'
// 简易模版函数 function template(tmpl, data) { var result = tmpl; for (var key in data) { result = result.replace(new RegExp("\\(" + key + "\\)", "g"), data[key]); } return result; } let me = template("个人名字是(name),个人工做是(work),(name) Love (work)", { name: "xxx", work: "yy" }); console.log(me);
function repeat(func, times, wait) { // TODO } const repeatFunc = repeat(alert, 4, 3000); repeatFunc("hellworld"); //会alert4次 helloworld,每次间隔3秒
function repeat(func, times, wait) { return message => { let timer = setInterval(() => { times-- > 0 ? func(message) : clearInterval(timer); }, wait); }; } const repeatFunc = repeat(console.log, 4, 3000); repeatFunc("hellworld");
我手中有一堆扑克牌, 可是观众不知道它的顺序。
第一步, 我从牌顶拿出一张牌, 放到桌子上。
第二步, 我从牌顶再拿一张牌, 放在手上牌的底部。
第三步, 重复第一步、第二步的操做, 直到我手中全部的牌都放到了桌子上。
最后, 观众能够看到桌子上牌的顺序是:(牌底部)1,2,3,4,5,6,7,8,9,10,11,12,13(牌顶部)
请问, 我刚开始拿在手里的牌的顺序是什么?
请编程实现。前端
/** * Input 拿出牌的顺序 1,2,3,4,5,6,7,8,9,10,11,12,13 * Output 牌堆原来的顺序 */ function getCardsOrder(input, cards) { //Swap if (cards.length) { let popCard = cards.pop(); cards.unshift(popCard); } //Push let popItem = input.pop(); cards.unshift(popItem); console.log(`Popitem: ${popItem}`); console.log(`inputAfterPop: ${input}`); console.log(`Cards ${cards}`); console.log(""); if (input.length == 0) { return cards; } else { return getCardsOrder(input, cards); } } let input = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]; let test = [1, 3, 5, 4, 2]; //1,2,3,4,5 let test2 = [1, 3, 2]; //1,2,3 let callback = getCardsOrder(input, []); console.log(callback);