기본 콘텐츠로 건너뛰기

2월, 2019의 게시물 표시

JS 5.7 task1 Filter unique array members

<HTML>   <body>   </body>    <script> function unique(arr){   return Array.from(new Set(arr)); } let values = ["Hare", "Krishna", "Hare", "Krishna",   "Krishna", "Krishna", "Hare", "Hare", ":-O" ]; //alert( unique(values) ); // Hare, Krishna, :-O alert(unique(values));   </script> </HTML> <!-- //not likely with object, map does not change the key to be string //map can also use object as keys my solution let uniqueSet = new Set(); for(let item of arr){   uniqueSet.add(item); } for(let value of uniqueSet) alert(value); //the condition of the task was returning a real arary i wasnt aware of it lol -->

JS 5.6 Iterables

<HTML>   <body>   </body>    <script>    let range = {   from: 1,   to: 5,     [Symbol.iterator]() {     this.current = this.from;     return this;   },     next() {     if (this.current <= this.to) {       return { done: false, value: this.current++ };     } else {       return { done: true };     }   } }; let arr = Array.from(range, num => num * num); //second argument arrows each member of the object alert(arr);   </script> </HTML> //itearble means an object with Symbol.iterator and next() //so that it can be used for for - of and array.from //array-like is an object, which have an index for each item //and have an member length //string has both traits of arraylike and iterable, //but normally both trait cannot stand together in one object

JS 5.5 task 12 Filter unique array members

<HTML>   <body>   </body>    <script> function unique(arr){   let result = [];   for(let item of arr){     if (!result.includes(item)) result.push(item);   }   return result; } let strings = ["Hare", "Krishna", "Hare", "Krishna",   "Krishna", "Krishna", "Hare", "Hare", ":-O" ]; alert( unique(strings) ); // Hare, Krishna, :-O   </script> </HTML> <!-- my answer function unique(arr) {   let unique = [];   arr.forEach(     function (item){       if(!unique.includes(item)){         unique.push(item);       }     }   );   return unique; } -->

JS 5.5 task 11 Get average age

<HTML>   <body>   </body>    <script>    let john = { name: "John", age: 25 };    let pete = { name: "Pete", age: 30 };    let mary = { name: "Mary", age: 29 };    let arr = [ john, pete, mary ]; function getAverageAge(arr){   return arr.reduce((sum, curr) => sum + curr.age, 0) / arr.length; } alert(getAverageAge(arr));   </script> </HTML> <!-- //My answer function getAverageAge(arr){   let sum = 0;   arr.forEach((item) => {     sum = sum + item.age;   });   return sum / arr.length; } -->

JS 5.5 task10 Shuffle an array

<HTML>   <body>   </body>    <script>    let arr = [1,2,3];    function shuffle(array){      array.sort(() => Math.random() - 0.5);      //since math.random() returns a number between 0 and 1      //by - 0.5, it can be a negative or positive      //and the sorting function compares      //each element by that randomly generated returned value    }    shuffle(arr);    alert(arr);    shuffle(arr);    alert(arr);    shuffle(arr);    alert(arr);   </script> </HTML>

JS 5.5 task9 Sort objects

<HTML>   <body>   </body>    <script>    let john = { name: "John", age: 25 };    let pete = { name: "Pete", age: 30 };    let mary = { name: "Mary", age: 28 };    let arr = [ john, pete, mary ]; function sortByName(arr){     arr.sort((a,b) => {     if(a.age > b.age) return 1;     if(a.age == b.age) return 0;     if(a.age < b.age) return -1;   }); } sortByName(arr); alert(arr[0].name); // John alert(arr[2].name); // Pete   </script> </HTML>

JS 5.5 task8 Map to objects

<HTML>   <body>   </body>    <script>    let john = { name: "John", surname: "Smith", id: 1 };    let pete = { name: "Pete", surname: "Hunt", id: 2 };    let mary = { name: "Mary", surname: "Key", id: 3 };    let users = [ john, pete, mary ];    let usersMapped = users.map(function(item){    let obj = {    fullName : item.name + " " + item.surname,    id : item.id  };    return obj;  });  alert( usersMapped[0].id ) // 1  alert( usersMapped[0].fullName ) // John Smith   </script> </HTML> <!-- solution coding let usersMapped = users.map(user => ({ fullName: `${user.name} ${user.surname}`, id : user.id })); 1. uses shorten version of function writing 2. () is to say what it does every term of mapping,     is calling this anonymous function which make and return     the objeect mapped 3. {} means this function will return a object!!! -->

JS 5.5 task7 Map to names

<HTML>   <body>   </body>    <script>    let john = { name: "John", age: 25 };    let pete = { name: "Pete", age: 30 };    let mary = { name: "Mary", age: 28 };    let users = [ john, pete, mary ]; let names = users.map((item => {   return item.name; })) alert(names);   </script> </HTML>