摘要:數據集合基礎數據保存基本數據排序操作校驗越大優先級越高多屬性比較原始數據季度相關數據金額保留的小數位數增加平均值春夏秋冬構造自增長度代碼的健壯性,很大一部分工作在入口的嚴格把控入參的處理這里會有一個通用的工具方法集合
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.
/* * 數據集合 * 基礎數據保存 + 基本數據排序操作 */ class ExtensionsSort { constructor (data = []) { this.data = data; this.extTypeList = ["DigitalUser", "VirtualUser","FaxUser", "AO", "Dept"]; } // 校驗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越大優先級越高 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){ // 原始數據 this.saleItems = saleItems; // 季度相關數據 this.sumOfQuarterItems = []; //金額保留的小數位數 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{ // 構造 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]
代碼的健壯性,很大一部分工作在入口的嚴格把控【入參的處理】
這里會有一個通用的工具方法集合
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"); } }
文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。
轉載請注明本文地址:http://m.specialneedsforspecialkids.com/yun/90315.html
摘要:題目一題目什么是閉包,為什么要用它網址博客題目二題目的實現原理網址題目三題目是什么的交互模型同步和異步的區別如何解決跨域問題網址題目四題目如何解決跨域問題網址題目五題目原生封裝處理兼容網址 題目一:題目:什么是閉包(closure),為什么要用它?網址:http://bbs.daxiangclass.com/?...博客:https://www.jianshu.com/p/6fa......
摘要:定義變量如果不使用則變量為為全局作用域。當然嚴格模式是禁止這樣做的。遵循詞法作用域原則,其中后兩題來源于權威指南。非箭頭函數下的指向運行時所在作用域。中逗號操作符會從左到右計算它的操作數,返回最后一個操作數的值。原文發表于我的博客 (function(){ var a = b =1; })() console.log(b) 答案:1。定義變量如果不使用 var 則變量為為全局作...
閱讀 3730·2021-11-24 09:39
閱讀 1891·2021-11-16 11:45
閱讀 620·2021-11-16 11:45
閱讀 1040·2021-10-11 10:58
閱讀 2484·2021-09-09 11:51
閱讀 1946·2019-08-30 15:54
閱讀 695·2019-08-29 13:13
閱讀 3472·2019-08-26 12:18