以前的一篇主要是 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);