//1 var myArr = [1,2,3,4,5,6]; Array.prototype.last = function() { return this[this.length - 1] } Array.prototype.skip = function(n) { return res = this.slice(n) } Array.prototype.take = function(n) { return res = this.slice(0,n) } Array.prototype.sum = function() { return res = this.reduce((a,b) => a + b) } Array.prototype.avarage = function() { return res = this.reduce((a,b) => a + b) / this.length } console.log(myArr.last()); console.log(myArr.skip(2)); console.log(myArr.take(3)); console.log(myArr.sum()); console.log(myArr.avarage()); //2 var obj = { weight: 80, experience: 1, bloodAlc: 0, handSh: true } const addAl = (obj) => { if(obj.handSh) { obj.bloodAlc = obj.bloodAlc + obj.weight/10*obj.experience obj.handSh = false } return obj } console.log(addAl(obj)); //3 var car = { model: 'VW Golf II', power: 160, color: 'blue', carriage: 'hatchback', weelsize: 15 } const small = { power: 90, volume: 1800 } const normal = { power: 120, volume: 2400 } const monster = { power: 200, volume: 3500 } const carSelect = (obj) => { var res = {} res.model = obj.model res.engine = closer(obj.power) res.carriage = { type: obj.carriage, color: obj.color} if (obj.weelsize % 2 === 0) res.wheels = [obj.weelsize - 1,obj.weelsize - 1,obj.weelsize - 1,obj.weelsize - 1] else res.wheels = [obj.weelsize,obj.weelsize,obj.weelsize,obj.weelsize] return res } const closer = (c) => { c1 = Math.abs(c-small.power) c2 = Math.abs(c-normal.power) c3 = Math.abs(c-monster.power) const min = Math.min.apply(null, [c1,c2,c3]) if(min === c1) return small else if (min === c2) return normal else return monster } console.log(carSelect(car)); //4 function getExtendableObject() { let obj = {} obj = Object.create(obj) obj.extend = function(template) { for (var key in template) { if (template.hasOwnProperty(key)) { var element = template[key]; if (typeof element === 'function') { obj.__proto__[key] = element } else { obj[key] = element } } } } return obj } let template = { extensionMethod: function() { console.log('gosho') }, extensionProperty: 'someString' } //let obj = getExtendableObject() //obj.extend(template) console.log(obj.__proto__) //5 String.prototype.ensureStart = function(str) { let modifiedStr = this.toString() if (this.substr(0, str.length) !== str) { modifiedStr = str + this } return modifiedStr } String.prototype.ensureEnd = function(str) { let modifiedStr = this.toString() if (this.substr(this.length - str.length) !== str) { modifiedStr += str } return modifiedStr } String.prototype.isEmpty = function() { return this.length === 0 } String.prototype.truncate = function(n) { if (n < 4) { return '.'.repeat(n) } if (n >= this.length) { return this.toString() } let lastIndexOfSpaceBeforeN = -1 let currentIndex = -1 currentIndex = this.indexOf(' ', currentIndex + 1) while (currentIndex !== -1 && currentIndex <= n - 3) { lastIndexOfSpaceBeforeN = currentIndex currentIndex = this.indexOf(' ', currentIndex + 1) } if (lastIndexOfSpaceBeforeN !== -1) { return this.substr(0, lastIndexOfSpaceBeforeN) + '...' } else { return this.substr(0, n - 3) + '...' } } String.format = function(inputString) { let match let formattedString = inputString let pattern = /{(\d)}/g while (match = pattern.exec(inputString)) { let parameterIndex = Number(match[1]) + 1 if (parameterIndex < arguments.length) formattedString = formattedString.replace(match[0], arguments[parameterIndex]) } return formattedString } let str = 'my string' str = str.ensureStart('my') console.log(str) str = str.ensureStart('hello ') console.log(str) str = str.truncate(16) console.log(str) str = str.truncate(14) console.log(str) str = str.truncate(8) console.log(str) str = str.truncate(4) console.log(str) str = str.truncate(2) console.log(str) str = String.format('The {0} {1} fox', 'quick', 'brown'); console.log(str) str = String.format('jumps {0} {1}', 'dog'); console.log(str) //6 function getSortedCollection() { return { collection: [], sort: function() { this.collection = this.collection.sort((a, b) => a - b); }, add: function(element) { this.collection.push(element); this.size++; this.sort(); }, toString: function() { return this.collection.join(', '); }, remove: function(index) { if (index >= 0 && index < this.size) { this.collection.splice(index, 1); this.size--; } }, get: function(index) { if (index >= 0 && index < this.size) return this.collection[index]; }, size: 0 } } let collection = getSortedCollection(); collection.add(0) collection.add(2) collection.add(106) collection.add(0) collection.remove(2) console.log(collection.get(2)) console.log(collection.size) console.log(collection.toString())