기본 콘텐츠로 건너뛰기

이 블로그의 인기 게시물

JS 5.5 task6 Create an extendable calculator

<HTML>   <body>   </body>   <script> function Calculator() {   let methods = {     "-" : (a, b) => a - b,     "+" : (a, b) => a + b   };   //methods is an object which keeps key for operators   //and value to return the actual operation values   //each returns the result of operation that key(operator) does   this.calculate = function (str){     //calculate is one element in the function Calculator     //it takes the string and returns the value     //in the function element list is delimeted by , not ;     let split = str.split(" "),     a = +split[0],     op = split[1],     b = split [2]     if(!methods[op] || isNaN(a) || isNaN(b)) {       return NaN; // error handling     }     return methods[op](a,b);   }   this.addMethod = function(name, func){     methods[name] = func;     //this is how to add new key and ele to object   } } let powerCalc = new Calculator; powerCalc.addMethod("*&

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 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> <!--    자세한 설명은 생략한다. 날씨 너무 좋으니까 좀 놀러나간다    근데 이해는 했으니까 괜찮음 ㅎ -->