update arraymethod and ieteratbles
This commit is contained in:
		| @@ -89,13 +89,92 @@ console.log(numbers.reduceRight((sum, cur) => sum + cur, 0)); | |||||||
| console.log(Array.isArray(countries)) | console.log(Array.isArray(countries)) | ||||||
|  |  | ||||||
| function camelize(str){ | function camelize(str){ | ||||||
|     words = str.split('-') |     // words = str.split('-') | ||||||
|     for(let i = 1; i < words.length; i++){ |     // for(let i = 1; i < words.length; i++){ | ||||||
|         words[i][0] = words[i][0].toUpperCase(); |     //     words[i] = words[i][0].toUpperCase() + words[i].slice(1); | ||||||
|         console.log(words[i]) |     // } | ||||||
|     } |     // return words.join(''); | ||||||
|     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("background-color")) | ||||||
| console.log(camelize("list-style-image")) | console.log(camelize("list-style-image")) | ||||||
| console.log(camelize("-webkit-translation")) | 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)) | ||||||
| @@ -53,3 +53,6 @@ console.log(arr_s); | |||||||
| console.log(arr_s[0]); | console.log(arr_s[0]); | ||||||
| console.log(arr_s[1]); | console.log(arr_s[1]); | ||||||
| console.log(arr_s.length); | console.log(arr_s.length); | ||||||
|  |  | ||||||
|  |  | ||||||
|  |  | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user