Browse Source

HW js09 fixed done

Ivar 2 năm trước cách đây
mục cha
commit
df0aa51cca

+ 24 - 10
js/09_closures/tasks/index.js

@@ -3,10 +3,13 @@
 function makeProfileTimerWrapp() {
 
    function makeProfileTimer() {
+      let time1 = performance.now()
       return function () {
-         return performance.now()
+         let time2 = performance.now()
+         return time2 - time1
       } 
    }
+
    let timer = makeProfileTimer()
    alert('Замеряем время работы этого alert')
    alert(timer())
@@ -17,8 +20,10 @@ function makeSaverWrapp() {
 
    function makeSaver(func) {
       let storage = null
+      let label = false
       return function () {
-         if (storage === null) {
+         if (label === false) {
+            label = true
             storage = func()
             return storage
          } else {
@@ -33,11 +38,19 @@ function makeSaverWrapp() {
    console.log(value1) 
    console.log(value2)
    console.log(value1 === value2) 
+   {
+      let saver2 = makeSaver(() => console.log('saved function called') || [null, undefined, false, '', 0, Math.random()][Math.floor(Math.random()*6)])
+      let value3 = saver2()
+      let value4 = saver2()
+      console.log(value3, value4) 
+   }
 
-   let saver2 = makeSaver(() => console.log('saved function called') || [null, undefined, false, '', 0, Math.random()][Math.ceil(Math.random()*6)])
-   let value3 = saver2()
-   let value4 = saver2()
-   console.log(value3 === value4) 
+   {
+      let saver2 = makeSaver(() => console.log('saved function called') || [null, undefined, false, '', 0, Math.random()][Math.floor(Math.random()*6)])
+      let value3 = saver2()
+      let value4 = saver2()
+      console.log(value3, value4) 
+   }
 }
 
 // finalCountdownWrapp()
@@ -56,19 +69,20 @@ function finalCountdownWrapp() {
 function myBindWrapp() {
 
    function myBind(func, funcThis, array) {
-      return function () {
+      return function (...newParams) {
          let args = () => {
             let newArr = []
             let i = 0
             for (const el of array) {
                if (el === undefined) {
-                  newArr.push(arguments[i])
+                  newArr.push(newParams[i])
                   i++
                } else {
                   newArr.push(el)
                }
             }
-            return newArr
+            let restParams = newParams.slice(i)
+            return [...newArr, ...restParams]
          }
          // console.log(args())         
          return func.apply(funcThis, args())
@@ -90,7 +104,7 @@ function myBindWrapp() {
    let someNumber = zeroPrompt("Введите число")  
 
    myBind((...params) => params.join(''), null, [undefined, 'b', undefined, undefined, 'e', 'f'])('a','c','d') === 'abcdef'
-   console.log(myBind((...params) => params.join(''), null, [undefined, 'b', undefined, undefined, 'e', 'f'])('a','c','d'))   
+   console.log(myBind((...params) => params.join(''), null, [undefined, 'b', undefined, undefined, 'e', 'f'])('a','c','d','UU','AA'))   
 }
 
 

+ 54 - 11
js/20_canvas-2/index.js

@@ -217,6 +217,24 @@ class Line extends Drawable {
         ctx.lineWidth   = this.lineWidth
         ctx.stroke();
     }
+
+
+
+    in(x,y){
+        let lineAngle = Math.atan2(this.height, this.width);
+        let mouseAngle = Math.atan2(y - this.y, x - this.x);
+        let lineTurnAngle = mouseAngle - lineAngle;
+        let rotX = Math.cos(lineTurnAngle) * distance(x,y,this.x,this.y);
+        let rotY = Math.sin(lineTurnAngle) * distance(x,y,this.x,this.y);
+        
+        if (rotX > 0 && rotX < this.distanceTo(this.x + this.width, this.y + this.height) && (rotY > -this.lineWidth / 2)
+        && (rotY < this.lineWidth / 2)) {
+            console.log(true)
+            return
+        } else {
+            console.log(false)
+        }
+    }
 }
 
 
@@ -233,6 +251,31 @@ class Rectangle extends Drawable {
         this.draw(); 
     }    
 
+    // invertColor(hex){
+    //     function padZero(str, len) {
+    //         len = len || 2;
+    //         var zeros = new Array(len).join('0');
+    //         return (zeros + str).slice(-len);
+    //     }
+    //     if (hex.indexOf('#') === 0) {
+    //         hex = hex.slice(1);
+    //     }
+    //     // convert 3-digit hex to 6-digits.
+    //     if (hex.length === 3) {
+    //         hex = hex[0] + hex[0] + hex[1] + hex[1] + hex[2] + hex[2];
+    //     }
+    //     if (hex.length !== 6) {
+    //         throw new Error('Invalid HEX color.');
+    //     }
+    //     // invert color components
+    //     var r = (255 - parseInt(hex.slice(0, 2), 16)).toString(16),
+    //         g = (255 - parseInt(hex.slice(2, 4), 16)).toString(16),
+    //         b = (255 - parseInt(hex.slice(4, 6), 16)).toString(16);
+    //     // pad each with zeros and return
+    //     return '#' + padZero(r) + padZero(g) + padZero(b);
+    // }  
+
+
     draw(selected){
         ctx.beginPath();
         ctx.moveTo(this.x, this.y);
@@ -240,13 +283,13 @@ class Rectangle extends Drawable {
         ctx.fillStyle = this.color;
         ctx.fillRect(this.x, this.y, this.width, this.height);
 
-        if (selected){           
+        if (selected){     
             ctx.rect(this.x, this.y, this.width, this.height);
-            ctx.closePath();
+            ctx.lineWidth = 2;
             ctx.stroke();
-        }
+            
+        } 
         ctx.closePath();
-
     }
 
 
@@ -314,7 +357,8 @@ class Ellipse extends Drawable {
         // console.log(this.ry)
         ctx.moveTo(this.x, this.y);
         ctx.fillStyle = this.color;
-        ctx.ellipse(this.x + this.width/2, this.y + this.height/2, this.width/2, this.height/2, 0, 0, 2 * Math.PI);
+        // ctx.ellipse(this.x + this.width/2, this.y + this.height/2, this.width/2, this.height/2, 0, 0, 2 * Math.PI);
+        ctx.ellipse(this.x + this.rx, this.y + this.ry, this.rx, this.ry, 0, 0, 2 * Math.PI);
         ctx.fill()
         ctx.closePath();
     }
@@ -338,7 +382,10 @@ document.getElementById('delete').onclick = () =>{
     Drawable.drawAll()
 }
 
-
+undo.onclick = function(){
+    Drawable.instances.pop()
+    Drawable.drawAll()
+}
 
 
 
@@ -348,11 +395,7 @@ document.getElementById('delete').onclick = () =>{
 ////canvas.onmousemove = function(e){
 ////}
 
-//undo.onclick = function(){
-    //Drawable.instances.pop()
-    ////Drawable.instances = []
-    //Drawable.drawAll()
-//}
+
 
 
 

js/21/index.html → js/21_generators/index.html


+ 51 - 0
js/21_generators/index.js

@@ -0,0 +1,51 @@
+
+// function* likeFor(end, step = 1, start = 0) {
+//     for(let i = start; i < end; i+=step) {
+//         yield i;
+//         console.log(i)
+//     }
+// }
+
+// let iter = likeFor(10)
+
+// function* multCall() {
+//    yield* likeFor(10)
+//    yield* likeFor(20)
+//    yield* 'СТОРКА'
+// }
+// let a = [...multCall()]
+// console.log(a)
+
+
+
+
+
+const delay = ms => new Promise(ok => setTimeout(() => ok(ms), ms))
+
+function* likeAsync(){
+    console.log(1)
+    let res1 = yield delay(2000)
+    console.log(2, res1)
+    let res2 = yield delay(2000)
+    console.log(3, res2)
+}
+
+function run(likeAsync) {
+    let iter = likeAsync()
+    function next(result, err) { 
+        let value, done
+        if (!err) {
+            [{value, done}] = [iter.next(result)]
+        } else {
+            [{value, done}] = [iter.throw(err)]
+        }
+        if (done) return;
+        if(value && typeof value?.then === 'function') {
+            value.then(next, err => next(undefined, err))
+        } else {
+            next(value)
+        }
+    }
+    next()
+}
+run(likeAsync)

+ 13 - 0
js/24/index.html

@@ -0,0 +1,13 @@
+<!DOCTYPE html>
+<html lang="en">
+<head>
+   <meta charset="UTF-8">
+   <meta http-equiv="X-UA-Compatible" content="IE=edge">
+   <meta name="viewport" content="width=device-width, initial-scale=1.0">
+   <title>Document</title>
+</head>
+<body>
+   
+   <script src="./index.js"></script>
+</body>
+</html>

js/21/index.js → js/24/index.js


+ 13 - 0
js/25/index.html

@@ -0,0 +1,13 @@
+<!DOCTYPE html>
+<html lang="en">
+<head>
+   <meta charset="UTF-8">
+   <meta http-equiv="X-UA-Compatible" content="IE=edge">
+   <meta name="viewport" content="width=device-width, initial-scale=1.0">
+   <title>Document</title>
+</head>
+<body>
+   
+   <script src="./index.js"></script>
+</body>
+</html>

+ 24 - 0
js/25/index.js

@@ -0,0 +1,24 @@
+htmlTree()
+function htmlTree() {
+
+}
+
+htmlTree()
+function htmlTree() {
+
+}
+
+htmlTree()
+function htmlTree() {
+
+}
+
+htmlTree()
+function htmlTree() {
+
+}
+
+htmlTree()
+function htmlTree() {
+
+}