Compare commits
3 Commits
b70d452341
...
138644d870
Author | SHA1 | Date | |
---|---|---|---|
|
138644d870 | ||
|
29dd89f729 | ||
|
5fe9b2c913 |
5
.gitattributes
vendored
Normal file
5
.gitattributes
vendored
Normal file
@ -0,0 +1,5 @@
|
||||
# All line endings should be unix
|
||||
* text eol=lf
|
||||
|
||||
*.png binary
|
||||
*.jpg binary
|
16
journal/_src/posts/2022-09-28-lab-10.md
Normal file
16
journal/_src/posts/2022-09-28-lab-10.md
Normal file
@ -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
|
||||
<!-- more -->
|
||||
|
||||
## Methods
|
||||
|
||||
## Prototypes
|
||||
|
||||
## Classes
|
||||
|
||||
## Arrays
|
||||
|
||||
## Recursion
|
18
labs/L10/arrays.js
Normal file
18
labs/L10/arrays.js
Normal file
@ -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;
|
17
labs/L10/loop-arith.js
Normal file
17
labs/L10/loop-arith.js
Normal file
@ -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;
|
16
labs/L10/rec-arith.js
Normal file
16
labs/L10/rec-arith.js
Normal file
@ -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;
|
31
labs/L10/spec/arrays.spec.js
Normal file
31
labs/L10/spec/arrays.spec.js
Normal file
@ -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)
|
||||
});
|
||||
});
|
30
labs/L10/spec/rec_arith.spec.js
Normal file
30
labs/L10/spec/rec_arith.spec.js
Normal file
@ -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);
|
||||
});
|
||||
});
|
31
labs/L10/spec/rec_vs_loop.spec.js
Normal file
31
labs/L10/spec/rec_vs_loop.spec.js
Normal file
@ -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));
|
||||
});
|
||||
});
|
13
labs/L10/spec/support/jasmine.json
Normal file
13
labs/L10/spec/support/jasmine.json
Normal file
@ -0,0 +1,13 @@
|
||||
{
|
||||
"spec_dir": "spec",
|
||||
"spec_files": [
|
||||
"**/*[sS]pec.?(m)js"
|
||||
],
|
||||
"helpers": [
|
||||
"helpers/**/*.?(m)js"
|
||||
],
|
||||
"env": {
|
||||
"stopSpecOnExpectationFailure": false,
|
||||
"random": true
|
||||
}
|
||||
}
|
72
labs/L10/time.js
Normal file
72
labs/L10/time.js
Normal file
@ -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)
|
Loading…
Reference in New Issue
Block a user