From 9def6b47ed87eace46a436ab444110d94c4a2ad6 Mon Sep 17 00:00:00 2001 From: mhrooz Date: Fri, 21 Jun 2024 22:04:48 +0200 Subject: [PATCH] update arraymethod and ieteratbles --- 5.4ArrayMethod/t.js | 93 +++++++++++++++++++++++++++++++++++++++++---- 5.5Iterables/t.js | 3 ++ 2 files changed, 89 insertions(+), 7 deletions(-) diff --git a/5.4ArrayMethod/t.js b/5.4ArrayMethod/t.js index d2b5df4..7a0da45 100644 --- a/5.4ArrayMethod/t.js +++ b/5.4ArrayMethod/t.js @@ -89,13 +89,92 @@ console.log(numbers.reduceRight((sum, cur) => sum + cur, 0)); console.log(Array.isArray(countries)) function camelize(str){ - words = str.split('-') - for(let i = 1; i < words.length; i++){ - words[i][0] = words[i][0].toUpperCase(); - console.log(words[i]) - } - return words.join(''); + // words = str.split('-') + // for(let i = 1; i < words.length; i++){ + // words[i] = words[i][0].toUpperCase() + words[i].slice(1); + // } + // return words.join(''); + return str.split('-') + .map((word, index) => index == 0 ? word: word[0].toUpperCase() + word.slice(1)) + .join('') } console.log(camelize("background-color")) console.log(camelize("list-style-image")) -console.log(camelize("-webkit-translation")) \ No newline at end of file +console.log(camelize("-webkit-translation")) + +function filterRange(arr, a, b){ + return arr.filter(ele => ele >= a && ele <= b); +} +arr = [5, 3, 8, 1]; +let filtered = filterRange(arr, 1, 4); +console.log(filtered) +console.log(arr) + + +function copySorted(arr){ + return arr.slice().sort(); +} +arr = ["HTML", "JavaScript", "CSS"]; + +let sorted = copySorted(arr); +console.log(sorted) +console.log(arr) + +function Calculator(){ + this.methods = { + "-": (a,b) => a-b, + "+": (a,b) => a+b + }; + this.calculate = function(str){ + let split = str.split(' '); + a = +split[0]; + op = split[1]; + b = +split[2]; + + if (!this.methods[op] || isNaN(a) || isNaN(b)){ + return NaN; + } + + return this.methods[op](a,b); + } + this.addMethod = function(name, func){ + this.methods[name] = func; + } +} +let john = { name: "John", surname: "Smith", id: 25 }; +let pete = { name: "Pete", surname: "Hunt", id: 30 }; +let mary = { name: "Mary", surname: "Key", id: 28 }; + +users = [ john, pete, mary ]; + +names = users.map((user) => user.name) + +console.log( names ); // John, Pete, Mary + +let usersMapped = users.map((user) => { + return { + fullName: user.name + ' ' + user.surname, + id: user.id + } +} +); +console.log(usersMapped[0].id) +console.log(usersMapped[0].fullName) + +function sortByAge(arr){ + return arr.sort((userA, userB) => userA.id - userB.id) +} + + +sortByAge(users) +console.log(users[0].name) +console.log(users[1].name) +console.log(users[2].name) + +function getAverageAge(arr){ + return arr.reduce(function(sum, cur){ + sum = sum + cur.id; + },0) / arr.length; +} + +console.log(getAverageAge(users)) \ No newline at end of file diff --git a/5.5Iterables/t.js b/5.5Iterables/t.js index 9c8608f..303f15e 100644 --- a/5.5Iterables/t.js +++ b/5.5Iterables/t.js @@ -53,3 +53,6 @@ console.log(arr_s); console.log(arr_s[0]); console.log(arr_s[1]); console.log(arr_s.length); + + +