diff --git a/journal/_src/posts/2022-09-28-lab-ten.md b/journal/_src/posts/2022-09-28-lab-ten.md new file mode 100644 index 0000000..69cc6f8 --- /dev/null +++ b/journal/_src/posts/2022-09-28-lab-ten.md @@ -0,0 +1,16 @@ + Title: Lab Ten + Date: 2022-10-17T08:30:00 + Tags: cs2613, lab, javascript, methods, tests, classes, prototypes, arrays, recursion + +In this lab, I learned about classes, prototypes, arrays and how to use recursion in javascript + + +## Methods + +## Prototypes + +## Classes + +## Arrays + +## Recursion \ No newline at end of file diff --git a/labs/L10/arrays.js b/labs/L10/arrays.js new file mode 100644 index 0000000..87123d2 --- /dev/null +++ b/labs/L10/arrays.js @@ -0,0 +1,18 @@ +function range(start, end, step=1) { + let result = []; + for (let i = start; i <= end; i += step) { + result.push(i); + } + return result; +} + +function sum(array) { + let result = 0; + for (let i = 0; i < array.length; i++) { + result += array[i]; + } + return result; +} + +exports.range = range; +exports.sum = sum; \ No newline at end of file diff --git a/labs/L10/loop-arith.js b/labs/L10/loop-arith.js new file mode 100644 index 0000000..294759d --- /dev/null +++ b/labs/L10/loop-arith.js @@ -0,0 +1,17 @@ +function plus(a,b) { + for (let i=0; i < a; i++){ + b++; + } + return b; +} + +function mult(a,b) { + sum=0; + for(let i=0; i < a; i++) { + sum = plus(sum, b) + } + return sum; +} + +exports.plus = plus; +exports.mult = mult; \ No newline at end of file diff --git a/labs/L10/rec-arith.js b/labs/L10/rec-arith.js new file mode 100644 index 0000000..1f55db9 --- /dev/null +++ b/labs/L10/rec-arith.js @@ -0,0 +1,16 @@ +function plus(a,b) { + if (a === 0) { + return b; + } else { + return plus(a-1, b+1); + } +} +function mult(a,b) { + if (a === 0) { + return 0; + } else { + return plus(mult(a-1, b), b); + } +} +exports.plus = plus; +exports.mult = mult; \ No newline at end of file diff --git a/labs/L10/spec/arrays.spec.js b/labs/L10/spec/arrays.spec.js new file mode 100644 index 0000000..a6bf796 --- /dev/null +++ b/labs/L10/spec/arrays.spec.js @@ -0,0 +1,31 @@ +let array=require("../arrays.js"); + +describe("range", function () { + it("empty", function () { + expect(array.range(2,1)).toEqual([]); + }); + it("single", function () { + expect(array.range(2,2)).toEqual([2]); + }); + it("multiple", function () { + expect(array.range(42,50)).toEqual([42,43,44,45,46,47,48,49,50]); + }); + it("step", function() { + expect(array.range(2,10,2)).toEqual([2,4,6,8,10]); + }) +}); + +describe("sum", function () { + it("empty", function () { + expect(array.sum([])).toBe(0); + }); + it("single", function () { + expect(array.sum([2])).toBe(2); + }); + it("multiple", function () { + expect(array.sum(array.range(1,10))).toBe(55); + }) + it("stepped", function() { + expect(array.sum(array.range(2,10,2))).toBe(30) + }); +}); \ No newline at end of file diff --git a/labs/L10/spec/rec_arith.spec.js b/labs/L10/spec/rec_arith.spec.js new file mode 100644 index 0000000..7d0fd62 --- /dev/null +++ b/labs/L10/spec/rec_arith.spec.js @@ -0,0 +1,30 @@ +let arith=require ("../rec-arith.js"); + +describe("plus", + function() { + it("1 + 1 = 2", + function() { + expect(arith.plus(1, 1)).toBe(2); + }); + it("0 + x = x", + function() { + expect(arith.plus(0, 1)).toBe(1); + }); + }); + +describe("mult", + function() { + it("0 * 2 = 0", + function() { + expect(arith.mult(0, 2)).toBe(0); + }); + + it("1 * 2 = 2", + function() { + expect(arith.mult(1,2)).toBe(2); + }); + it("2 * 2 = 4", + function() { + expect(arith.mult(2, 2)).toBe(4); + }); + }); \ No newline at end of file diff --git a/labs/L10/spec/rec_vs_loop.spec.js b/labs/L10/spec/rec_vs_loop.spec.js new file mode 100644 index 0000000..191dae5 --- /dev/null +++ b/labs/L10/spec/rec_vs_loop.spec.js @@ -0,0 +1,31 @@ +let loop_arith=require ("../loop-arith.js"); +let rec_arith=require ("../rec-arith.js"); + +describe("plus", + function() { + it("1 + 1 = 2", + function() { + expect(loop_arith.plus(1, 1)).toBe(rec_arith.plus(1, 1)); + }); + it("0 + x = x", + function() { + expect(loop_arith.plus(0, 1)).toBe(rec_arith.plus(0, 1)); + }); + }); + +describe("mult", + function() { + it("0 * 2 = 0", + function() { + expect(loop_arith.mult(0, 2)).toBe(rec_arith.mult(0, 2)); + }); + + it("1 * 2 = 2", + function() { + expect(loop_arith.mult(1, 2)).toBe(rec_arith.mult(1, 2)); + }); + it("2 * 2 = 4", + function() { + expect(loop_arith.mult(2, 2)).toBe(rec_arith.mult(2, 2)); + }); + }); \ No newline at end of file diff --git a/labs/L10/spec/support/jasmine.json b/labs/L10/spec/support/jasmine.json new file mode 100644 index 0000000..6afe603 --- /dev/null +++ b/labs/L10/spec/support/jasmine.json @@ -0,0 +1,13 @@ +{ + "spec_dir": "spec", + "spec_files": [ + "**/*[sS]pec.?(m)js" + ], + "helpers": [ + "helpers/**/*.?(m)js" + ], + "env": { + "stopSpecOnExpectationFailure": false, + "random": true + } +} diff --git a/labs/L10/time.js b/labs/L10/time.js new file mode 100644 index 0000000..4e64419 --- /dev/null +++ b/labs/L10/time.js @@ -0,0 +1,72 @@ +function timePlus(time1, time2) { + let mins = (time1.mins + time2.mins) % 60; + let hours = time1.hours + time2.hours + Math.floor((time1.mins+time2.mins)/60); + return {'hours': hours, 'mins': mins}; +} +console.log(timePlus({hours: 10, mins:30}, {hours: 17, mins:47})); +function maketime(hours, mins){ + let plus=function (other) { + let raw=timePlus(this,other); + return maketime(raw.hours, raw.mins); + }; + + return { 'hours': hours, 'mins': mins, 'plus': plus}; +} + +let A=maketime(10, 30); +let B=maketime(17, 47); +let C=A.plus(B); +console.log(C); + +let protoTime = { + plus: function(other) { + let raw=timePlus(this,other); + return timeNew(raw.hours, raw.mins); + } +}; + +function timeNew(hours, mins) { + let obj=Object.create(protoTime); + obj.hours = hours; + obj.mins = mins; + return obj; +} + +D=timeNew(21,42); +E=timeNew(17,37); + +F=D.plus(E); +console.log(F); + +function Time(hours, mins){ + this.hours=hours; + this.mins=mins; +} + +Time.prototype.plus=function (other) { + let raw=timePlus(this,other); + return new Time(raw.hours, raw.mins); +} + +G = new Time(20,59); +H = new Time(11,11); + +I=G.plus(H); +console.log(I); + +class Time2 { + constructor(hours, mins){ + this.hours=hours; + this.mins=mins; + }; + plus(other) { + let raw=timePlus(this,other); + return new Time2(raw.hours, raw.mins); + } +} + +J= new Time2(5,30); +K= new Time2(11,55); + +L=J.plus(K); +console.log(L) \ No newline at end of file