기본 콘텐츠로 건너뛰기

3월, 2019의 게시물 표시

JS 6.6 task Sum with an arbitrary amount of brackets

<HTML>   <body>   </body>    <script>    function sum(a) {          let currentSum = a;          function f(b) {        currentSum += b;        return f;      }          f.toString = function() {        return currentSum;      };          return f;    }        alert( sum(1)(2) ); // 3    alert( sum(5)(-1)(2) ); // 6    alert( sum(6)(-1)(-2)(-3) ); // 0    alert( sum(0)(1)(2)(3)(4)(5) ); // 15     </script> </HTML> <!--    자세한 설명은 생략한다. 날씨 너무 좋으니까 좀 놀러나간다    근데 이해는 했으니까 괜찮음 ㅎ -->   

JS 6.6 task1 Set and decrease for counter

<HTML>   <body>   </body>    <script> function makeCounter(){   let count = 0;   function counter(input){     return count++;   }   counter.decrease = () => count--;   counter.set = value => count = value;   return counter; } let counter = makeCounter(); alert( counter() ); // 0 alert( counter() ); // 1 counter.set(10); // set the new count alert( counter() ); // 10 counter.decrease(); // decrease the count by 1 alert( counter() ); // 10 (instead of 11)    </script> </HTML> <!-- 80% of me 20% of solution fair enough -->

JS 6.3 task Army of functions

<HTML>   <body>   </body>    <script>    function makeArmy() {      let shooters = [];      let i = 0;      while (i < 10) {        let j = i; // so that each array elements has separated        //lexical environments which is generated by each run of while{}block        let shooter = function() { // shooter function          alert( j ); // should show its number        };        shooters.push(shooter);        i++;      }      return shooters;    }    let army = makeArmy();    army[0](); // the shooter number 0 shows 10    army[5](); // and number 5 also outputs 10...    // ... all shooters show 10 instead of their 0, 1, 2, 3...   </script> </HTML> <!-- I beat the solution this time again!!baaaam!! -->

JS 6.3 task Sort by field

<HTML>   <body>   </body>    <script>    let users = [      { name: "John", age: 20, surname: "Johnson" },      { name: "Pete", age: 18, surname: "Peterson" },      { name: "Ann", age: 19, surname: "Hathaway" }    ]; //users.sort((a, b) => a.name > b.name ? 1 : -1); users.sort(byField('name')); users.sort(byField('name')); //users.sort((a, b) => a.age > b.age ? 1 : -1); function byField(field){   return (a, b) => a[field] > b[field] ? 1 : -1; }   </script> </HTML> <!-- REMEMBER BITCHES THEY WORK THE SAME!!!!!!!!! alert(users[0]["name"]); alert(users[0].name); AND thanks for coming to my ted talk lol I feel apparently loosing every time when I open up solutions before I get my own answer. but cannot lie on that I love to see how the solution is clear, clever and better than mine remember AGAIN BITCHES THEY WORK T

JS 6.3 task Filter through function

<HTML>   <body>   </body>    <script>    let arr = [1, 2, 3, 4, 5, 6, 7];   function inBetween(a, b) {      return function(x) {        return x >= a && x <= b;      };    }    alert( arr.filter(inBetween(3, 6)) ); // 3,4,5,6    function inArray(arr) {      return function(x) {        return arr.includes(x);      };    }    alert( arr.filter(inArray([1, 2, 10])) ); // 1,2   </script> </HTML> <!-- my solutions function inBetween(a, b){   let label = -1;   return function(){     label++;     if(arr[label] >= a && arr[label] <= b ) return true;   }; } //where the fuck x comes from and how the hell anyone knows that is an iteral of array //yeah now i know how to use filter() better thats thw whole point //well yeah i could not think of the functino include absolutely my bad yes function inArray(inArr){     let count = 0;     return function(x){       if(x == inArr[count] &&am

JS 5.11 task2 Exclude backreferences

<HTML>   <body>   </body>    <script>    let room = {      number: 23    };    let meetup = {      title: "Conference",      occupiedBy: [{name: "John"}, {name: "Alice"}],      place: room    };    // circular references    room.occupiedBy = meetup;    meetup.self = meetup;    alert( JSON.stringify(meetup, function replacer(key, value) {      alert(`${key}: ${value}`);     return (key != "" && value == meetup) ? undefined : value;    }));    /* result should be:    {      "title":"Conference",      "occupiedBy":[{"name":"John"},{"name":"Alice"}],      "place":{"number":23}    }    */   </script> </HTML>

JS 5.11 task1 Turn the object into JSON and back

<HTML>   <body>   </body>    <script>    let user = {      name: "John Smith",      age: 35    };    let stringified =JSON.stringify(user); //   alert(stringified);    let parsed = JSON.parse(stringified); //   alert(parsed.name); //   alert(parsed.age);   </script> </HTML> <!-- let solution = JSON.parse(JSON.stringify(user)); -->

JS 5.10 task7 How many seconds till tomorrow?

<HTML>   <body>   </body>    <script> function getSecondsToTomorrow(){   let stamp = new Date();   let date = new Date(stamp.getFullYear(), stamp.getMonth(), stamp.getDate() + 1);   let diff = date.getTime() - stamp.getTime();   return Math.round(diff / 1000); // making seconds   //getTime returns miliseconds } alert(getSecondsToTomorrow());   </script> </HTML>

JS 5.10 task6 How many seconds has passed today?

<HTML>   <body>   </body>    <script> function getSecondsToday(){   let stamp = new Date();   let date = new Date(stamp.getFullYear(), stamp.getMonth(), stamp.getDate()); //following two lines are from solution   let diff = stamp.getTime() - date.getTime();   return Math.round(diff / 1000); // making seconds   //getTime returns miliseconds } alert(getSecondsToday());   </script> </HTML>

JS 5.10 task5 Last day of month?

<HTML> <body> </body> <Script> function getLastDayOfMonth(year, month){   let date = new Date(year, month + 1, 0);   return date.getDate();   //putting 0 on day roll back the month   //and set the date to be the last date of the month } alert(getLastDayOfMonth(2012, 1)); </Script> </HTML> <!-- omg this guys are so smart function getLastDayOfMonth(year, month){   let date = new Date(year, month + 1);   date.setDate(date.getDate() - 1 );   return date.getDate(); } -->

JS 5.10 task4 which day of month was many days ago?

<HTML> <body> </body> <Script> function getDateAgo(date, num){   let dateSet = new Date(date);   dateSet.setDate(dateSet.getDate() - num);   return dateSet; } let date = new Date(2015, 0, 2);// 2 jan alert( getDateAgo(date, 1) ); // 1, (1 Jan 2015) alert( getDateAgo(date, 2) ); // 31, (31 Dec 2014) alert( getDateAgo(date, 365) ); // 2, (2 Jan 2014) </Script> </HTML>

JS 5.10 task3 European weekday

<HTML> <body> </body> <Script> function getLocalDay(date){   let day = date.getDay();   if(day == 0){     day = 7;   }   return day; } let date = new Date(2012, 0, 3);  // 3 Jan 2012 alert( getLocalDay(date) );       // tuesday, should show 2 <!-- well i gave it up lol--> </Script> </HTML>

JS 5.10 task2

<HTML> <body> </body> <Script> function getWeekDay(dateObj){   let day = ['SU','MO','TU','WE','TH','FR','SA'];   return day[dateObj.getDay()]; } let date = new Date(2012, 0, 3);  // 3 Jan 2012 alert( getWeekDay(date) ); </Script> </HTML> <!-- switch (day){   case 0 : return "SUN"; break;   case 1 : return "MON"; break;   case 2 : return "TUE"; break;   case 3 : return "WED"; break;   case 4 : return "THU"; break;   case 5 : return "FRI"; break;   case 6 : return "SAT"; break; } //im not shamed guys lol -->

JS 5.9 task2 The maximal salary

<HTML> <body> </body> <Script> let salaries = {   "John": 100,   "Pete": 300,   "Mary": 250 }; function topSalary(obj){ let max = 0; let maxName = null; for(const [name, sal] of Object.entries(obj)){   if(max < sal){     max = sal;     maxName = name;   } } return maxName; } alert(topSalary(salaries)); </Script> </HTML> <!-- {   if(Object.getOwnPropertyNames(obj).length == 0){     return null;   }   else{     let top = {       name : "",       sal : 0     };     for(let [key, value] of Object.entries(obj)){     if(value > top.sal){       top.name = key;       top.sal = value;     }     }     return top.name;   } } //its frustrating regardless of how proud I was, the solution one is better and fancier lol -->

JS 5.8 task1 Sum the properties

<HTML>   <body>   </body>    <script>    let salaries = {      "John": 100,      "Pete": 300,      "Mary": 250    };    function sumSalaries(obj){      let target = Object.values(obj);      let sum = 0;      for(let salary of target){        sum = sum + salary;      }      return sum;    }    alert( sumSalaries(salaries) ); // 650   </script> </HTML> <!-- solution answer let sum = 0; for (let salary of Object.values(obj)){ sum += salary } -->

JS 5.7 task5 Store read dates

<HTML>   <body>   </body>    <script>    let messages = [        {text: "Hello", from: "John"},        {text: "How goes?", from: "John"},        {text: "See you soon", from: "Alice"}    ];    let readMap = new WeakMap();    alert(readMap.size);    readMap.set(messages[0], new Date(2019, 3, 5));   </script> </HTML> <!-- task4 needed weakSet to save simply readmessage, this task needs to save THE TIME IT WAS READ along with the message itself the message out of the set or map means it hasn't been read I kinda feel good and bad at the same time to happen to read the solution but I do get to think more about the difference with tasks and be more available to understand the main contents so I think, its good? -->

JS 5.7 task 2 Filter anagrams

<HTML>   <body>   </body>    <script>    function aclean(arr){      let map = new Map();      for(let word of arr){        let sorted = word        .toLowerCase() // delete the case difference        .split('') //split it to sort        .sort() // sort it to delete the order difference        .join(''); // join to use as a key = one word        map.set(sorted, word);        //set saves keys by the key not by the value,        //the words canoot be saved when word with the same        //key is saved already        //map = is a collection of keyed value        //set = is a colelction of unique value      }       return Array.from(map.values());     }    let arr = ["nap", "teachers", "cheaters", "PAN", "ear", "era", "hectares"];    alert( aclean(arr) );   </script> </HTML> <!-- could not actually solve it but the solution logic was in

JS 5.7 task3 Iterable keys

<HTML>   <body>   </body>    <script>    let map = new Map();    map.set("name", "John");    let keys = Array.from(map.keys());    // Error: keys.push is not a function    keys.push("more");    alert(keys);   </script> </HTML> <!-- -->