摘要:數(shù)據(jù)集合基礎(chǔ)數(shù)據(jù)保存基本數(shù)據(jù)排序操作校驗(yàn)越大優(yōu)先級越高多屬性比較原始數(shù)據(jù)季度相關(guān)數(shù)據(jù)金額保留的小數(shù)位數(shù)增加平均值春夏秋冬構(gòu)造自增長度代碼的健壯性,很大一部分工作在入口的嚴(yán)格把控入?yún)⒌奶幚磉@里會有一個(gè)通用的工具方法集合
extensions is an Array and each item has such format:
{firstName: "xxx", lastName: "xxx", ext: "xxx", extType: "xxx"}
lastName, ext can be empty, extType can only has "DigitalUser", "VirtualUser","FaxUser","Dept","AO".
Question 1: sort extensions by "firstName" + "lastName" + "ext" ASC
Question 2: sort extensions by extType follow these orders ASC
DigitalUser < VitrualUser < FaxUser < AO < Dept.
/* * 數(shù)據(jù)集合 * 基礎(chǔ)數(shù)據(jù)保存 + 基本數(shù)據(jù)排序操作 */ class ExtensionsSort { constructor (data = []) { this.data = data; this.extTypeList = ["DigitalUser", "VirtualUser","FaxUser", "AO", "Dept"]; } // 校驗(yàn)extType checkExtType (item) { if (this.extTypeList.indexOf(item) === -1) { console.log("extType can only has "DigitalUser", "VirtualUser","FaxUser","Dept","AO""); return false; } return true; } compareExtType (type1, type2, isAsc) { // extTypeList index越大優(yōu)先級越高 let idx1 = this.extTypeList.indexOf(type1); let idx2 = this.extTypeList.indexOf(type2); if (idx1 === -1 || idx2 === -1) { console.log("extType can only has "DigitalUser", "VirtualUser","FaxUser","Dept","AO""); } return this.compare(idx1, idx2, isAsc); } // 多屬性比較 compareAttrs (el1, el2, attrs) { // attrs: [{key: "attr1", isAsc: false}] let res = []; let len = attrs.length; for (let i = 0; i < len; i++) { let val = this.compare(el1[attrs[i].key], el2[attrs[i].key], attrs[i].isAsc); res.push(val); if (val === 1 || val === -1) break; } for (let j = 0; j < res.length; j++) { if (res[j] === 1 || res[j] === -1) return res[j]; } return 0; } compare (item1, item2, isAsc) { if (item1 > item2) { return (isAsc ? 1 : -1); } else if (item1 < item2) { return (isAsc ? -1 : 1); } else { return 0; } } } function sortExtensionsByName(extensions) { let es = new ExtensionsSort(extensions); let attrs = [ { key: "firstName", isAsc: true }, { key: "lastName", isAsc: true }, { key: "ext", isAsc: true } ] extensions.sort((a, b) => { return es.compareAttrs(a, b, attrs); }); return extensions; } function sortExtensionsByExtType(extensions, isAsc = true) { let es = new ExtensionsSort(extensions); extensions.sort((a, b) => { return es.compareExtType(a, b, isAsc); }); return extensions; }
saleItems is an Array has each item has such format:
{ month: n,//[1-12], date: n, //[1-31], transationId: "xxx", salePrice: number}
Question 3: write a function to calculate and return a list of total sales (sum) for each quarter, expected result like:
[{quarter: 1, totalPrices: xxx, transactionNums: n},{....}]
Question 4: write a function to calculate and return a list of average sales for each quarter, expected result like:
[{quarter: 1, averagePrices: xxx, transactionNums: n},{....}]
class QuarterData { constructor(saleItems = [], digits = 2){ // 原始數(shù)據(jù) this.saleItems = saleItems; // 季度相關(guān)數(shù)據(jù) this.sumOfQuarterItems = []; //金額保留的小數(shù)位數(shù) this.digits = Math.pow(10, digits); this.quarterData(); return this; } quarterData () { let res = []; this.saleItems.forEach((item) => { let quarterId = this.getQuarter(item.month).quarter; if (!res[quarterId]) { res[quarterId] = { quarter: quarterId, totalPrices: 0, transactionNums: 0 }; } res[quarterId].totalPrices = (res[quarterId].totalPrices*this.digits + item.salePrice*this.digits)/this.digits; res[quarterId].transactionNums += 1; }); this.sumOfQuarterItems = res.filter((item) => { // 增加平均值 item ? (item.averagePrices = (item.totalPrices*this.digits / item.transactionNums) / this.digits) : ""; return item !== undefined; }); return this; } // quarter getQuarter (month) { let spring = 0; //春 let summer = 3; //夏 let fall = 6; //秋 let winter = 9; //冬 if (month < summer) { return { key: "spring", quarter: 1 }; } if (month < fall) { return { key: "summer", quarter: 2 }; } if (month < winter) { return { key: "fall", quarter: 3 }; } return { key: "winter", quarter: 3 }; } } function sumByQuarter(saleItems) { let sum = new QuarterData(saleItems).sumOfQuarterItems; let res = []; sum.forEach((item) => { res.push({ quarter: item.quarter, totalPrices: item.totalPrices, transactionNums: item.transactionNums }); }); return res; } function averageByQuarter(saleItems) { let average = new QuarterData(saleItems).sumOfQuarterItems; let res = []; average.forEach((item) => { res.push({ quarter: item.quarter, averagePrices: item.averagePrices, transactionNums: item.transactionNums }); }); return res; }
Question 5: please create a tool to generate Sequence Expected to be used like:
var sequence1 = new Sequence();
sequence1.next() --> return 1;
sequence1.next() --> return 2;
in another module:
var sequence2 = new Sequence();
sequence2.next() --> 3;
sequence2.next() --> 4;
let SequenceConfig = { idx: 0 }; class Sequence{ // 構(gòu)造 constructor(incr = 1){ // 自增長度 this.incr = incr; this.config = SequenceConfig; } incrIdx () { this.config["idx"] += this.incr; return this.config["idx"]; } next () { let idx = this.increaseIdx(); return idx; } }
Question 6:
AllKeys: 0-9; usedKeys: an array to store all used keys like [2,3,4]; We want to get an array which contains all the unused keys,in this example it would be: [0,1,5,6,7,8,9]
代碼的健壯性,很大一部分工作在入口的嚴(yán)格把控【入?yún)⒌奶幚怼?br>這里會有一個(gè)通用的工具方法集合
let _utils = { isNumber (data) { return Object.prototype.toString.call(data) === "[object Number]"; }, isArray (data) { return Object.prototype.toString.call(data) === "[object Array]"; }, createArray (number = 9) { let arr = []; for (let i = 0; i <= number; i++) { arr.push(i); } return arr; } }; function getUnUsedKeys (allKeys = 9, usedKeys = []) { _utils.isNumber(allKeys) ? allKeys = _utils.createArray(allKeys) : ""; if (_utils.isArray(allKeys)) { return allKeys.filter((item) => { return usedKeys.indexOf(item) === -1; }); } else { console.log("allKeys should be an array or number"); } }
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://specialneedsforspecialkids.com/yun/90315.html
摘要:題目一題目什么是閉包,為什么要用它網(wǎng)址博客題目二題目的實(shí)現(xiàn)原理網(wǎng)址題目三題目是什么的交互模型同步和異步的區(qū)別如何解決跨域問題網(wǎng)址題目四題目如何解決跨域問題網(wǎng)址題目五題目原生封裝處理兼容網(wǎng)址 題目一:題目:什么是閉包(closure),為什么要用它?網(wǎng)址:http://bbs.daxiangclass.com/?...博客:https://www.jianshu.com/p/6fa......
摘要:定義變量如果不使用則變量為為全局作用域。當(dāng)然嚴(yán)格模式是禁止這樣做的。遵循詞法作用域原則,其中后兩題來源于權(quán)威指南。非箭頭函數(shù)下的指向運(yùn)行時(shí)所在作用域。中逗號操作符會從左到右計(jì)算它的操作數(shù),返回最后一個(gè)操作數(shù)的值。原文發(fā)表于我的博客 (function(){ var a = b =1; })() console.log(b) 答案:1。定義變量如果不使用 var 則變量為為全局作...
摘要:全局環(huán)境調(diào)用函數(shù)的對象實(shí)際為,所以函數(shù)內(nèi)的指向構(gòu)造函數(shù)通過構(gòu)造函造函數(shù)生成了一個(gè)新對象,指向這個(gè)新對象。學(xué)習(xí)前端一個(gè)月,上一周面試了大概多家,收獲的卻是寥寥。為了效率,前端各方面的內(nèi)容都有涉獵,深度卻相當(dāng)不足,面試時(shí)暴露各種問題。 最近面試了不少家,苦于前端經(jīng)驗(yàn)薄弱,被各種血虐。做了不少家面試題,把各種不會的回來再做一遍,作為經(jīng)驗(yàn)總結(jié)吧。 1.如何最優(yōu)性能去重一個(gè)數(shù)組? 方法有好多,比...
摘要:全局環(huán)境調(diào)用函數(shù)的對象實(shí)際為,所以函數(shù)內(nèi)的指向構(gòu)造函數(shù)通過構(gòu)造函造函數(shù)生成了一個(gè)新對象,指向這個(gè)新對象。學(xué)習(xí)前端一個(gè)月,上一周面試了大概多家,收獲的卻是寥寥。為了效率,前端各方面的內(nèi)容都有涉獵,深度卻相當(dāng)不足,面試時(shí)暴露各種問題。 最近面試了不少家,苦于前端經(jīng)驗(yàn)薄弱,被各種血虐。做了不少家面試題,把各種不會的回來再做一遍,作為經(jīng)驗(yàn)總結(jié)吧。 1.如何最優(yōu)性能去重一個(gè)數(shù)組? 方法有好多,比...
閱讀 3723·2021-11-24 09:39
閱讀 1870·2021-11-16 11:45
閱讀 616·2021-11-16 11:45
閱讀 1028·2021-10-11 10:58
閱讀 2475·2021-09-09 11:51
閱讀 1941·2019-08-30 15:54
閱讀 687·2019-08-29 13:13
閱讀 3466·2019-08-26 12:18