Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stan tudor #30

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 64 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@
*/
function trimProperties(obj) {
// ✨ implement
const result = {};
for (let prop in obj) {
result[prop] = obj[prop].trim();
}
// console.log(result);
return result;
}

/**
Expand All @@ -20,6 +26,10 @@ function trimProperties(obj) {
*/
function trimPropertiesMutation(obj) {
// ✨ implement
for (let prop in obj) {
obj[prop] = obj[prop].trim();
}
return obj;
}

/**
Expand All @@ -31,7 +41,13 @@ function trimPropertiesMutation(obj) {
* findLargestInteger([{ integer: 1 }, { integer: 3 }, { integer: 2 }]) // returns 3
*/
function findLargestInteger(integers) {
// ✨ implement
let result = integers[0].integer;
for (let idx = 0; idx < integers.length; idx++) {
if (integers[idx].integer > result) {
result = integers[idx].integer;
}
}
return result;
}

class Counter {
Expand All @@ -41,6 +57,7 @@ class Counter {
*/
constructor(initialNumber) {
// ✨ initialize whatever properties are needed
this.count = initialNumber;
}

/**
Expand All @@ -56,7 +73,12 @@ class Counter {
* counter.countDown() // returns 0
*/
countDown() {
// ✨ implement
return this.count > 0 ? this.count-- : 0;
// or:
// if (this.count > 0) {
// return this.count--;
// }
// return this.count;
}
}

Expand All @@ -66,6 +88,8 @@ class Seasons {
*/
constructor() {
// ✨ initialize whatever properties are needed
this.seasons = ["summer", "fall", "winter", "spring"];
this.currentSeason = 0;
}

/**
Expand All @@ -82,6 +106,13 @@ class Seasons {
*/
next() {
// ✨ implement
const result = this.seasons[this.currentSeason];
if (this.currentSeason === 3) {
this.currentSeason = 0;
} else {
++this.currentSeason;
}
return result;
}
}

Expand All @@ -93,9 +124,11 @@ class Car {
* @param {number} mpg - miles the car can drive per gallon of gas
*/
constructor(name, tankSize, mpg) {
this.odometer = 0 // car initilizes with zero miles
this.tank = tankSize // car initiazes full of gas
this.odometer = 0; // car initilizes with zero miles
this.tank = tankSize; // car initiazes full of gas
// ✨ initialize whatever other properties are needed
this.tankSize = tankSize;
this.mpg = mpg;
}

/**
Expand All @@ -113,6 +146,15 @@ class Car {
*/
drive(distance) {
// ✨ implement
const milesCanDrive = this.tank * this.mpg;
if (distance <= milesCanDrive) {
this.odometer = this.odometer + distance;
this.tank = this.tank - distance / this.mpg;
} else {
this.tank = 0;
this.odometer = this.odometer + milesCanDrive;
}
return this.odometer;
}

/**
Expand All @@ -128,6 +170,12 @@ class Car {
*/
refuel(gallons) {
// ✨ implement
if (gallons <= this.tankSize - this.tank) {
this.tank = this.tank + gallons;
} else {
this.tank = this.tankSize;
}
return this.tank * this.mpg;
}
}

Expand All @@ -137,15 +185,22 @@ class Car {
* @returns {promise} - resolves true if number even, false otherwise
*
* EXAMPLE
* isEvenNumberAsync(2).then(result => {
* // result is true
* })
* isEvenNumberAsync(3).then(result => {
* // result is false
* })
*/
function isEvenNumberAsync(number) {
// ✨ implement
async function isEvenNumberAsync(number) {
if (typeof number !== "number" || isNaN(number)) {
throw new Error("error");
}
return number % 2 === 0 || false;
// ✨ or:
// if (number % 2 === 0) {
// return true;
// } else {
// return false;
// }
}

module.exports = {
Expand All @@ -156,4 +211,4 @@ module.exports = {
Counter,
Seasons,
Car,
}
};
218 changes: 163 additions & 55 deletions index.test.js
Original file line number Diff line number Diff line change
@@ -1,60 +1,168 @@
const utils = require('./index')
const utils = require("./index");

describe('[Exercise 1] trimProperties', () => {
test('[1] returns an object with the properties trimmed', () => {
test("this test works great", () => {
//empty test
});

describe("[Exercise 1] trimProperties", () => {
test("[1] returns an object with the properties trimmed", () => {
// EXAMPLE
const input = { foo: ' foo ', bar: 'bar ', baz: ' baz' }
const expected = { foo: 'foo', bar: 'bar', baz: 'baz' }
const actual = utils.trimProperties(input)
expect(actual).toEqual(expected)
})
// test('[2] returns a copy, leaving the original object intact', () => {})
})

describe('[Exercise 2] trimPropertiesMutation', () => {
// test('[3] returns an object with the properties trimmed', () => {})
// test('[4] the object returned is the exact same one we passed in', () => {})
})

describe('[Exercise 3] findLargestInteger', () => {
// test('[5] returns the largest number in an array of objects { integer: 2 }', () => {})
})

describe('[Exercise 4] Counter', () => {
let counter
const input = { foo: " foo ", bar: "bar ", baz: " baz" };
const expected = { foo: "foo", bar: "bar", baz: "baz" };
const actual = utils.trimProperties(input);
expect(actual).toEqual(expected);
});

test("[2] returns a copy, leaving the original object intact", () => {
const input = { foo: " foo ", bar: "bar ", baz: " baz" };
utils.trimProperties(input);
expect(input).toEqual({ foo: " foo ", bar: "bar ", baz: " baz" });
});
});

describe("[Exercise 2] trimPropertiesMutation", () => {
test("[3] returns an object with the properties trimmed", () => {
const input = { foo: " foo ", bar: "bar ", baz: " baz" };
const expected = { foo: "foo", bar: "bar", baz: "baz" };
const actual = utils.trimProperties(input);
expect(actual).toEqual(expected);
});
test("[4] the object returned is the exact same one we passed in", () => {
const input = { foo: " foo ", bar: "bar ", baz: " baz" };
const actual = utils.trimPropertiesMutation(input);
expect(actual).toBe(input);
});
});

describe("[Exercise 3] findLargestInteger", () => {
test("[5] returns the largest number in an array of objects { integer: 2 }", () => {
const input = [{ integer: 1 }, { integer: 3 }, { integer: 2 }];
const input2 = [{ integer: 4 }, { integer: 3 }, { integer: 2 }];
const input3 = [{ integer: 1 }, { integer: 3 }, { integer: 4 }];
const actual = utils.findLargestInteger(input);
const actual2 = utils.findLargestInteger(input2);
const actual3 = utils.findLargestInteger(input3);
expect(actual).toBe(3);
expect(actual2).toBe(4);
expect(actual3).toBe(4);
});
});

describe("[Exercise 4] Counter", () => {
let counter;
beforeEach(() => {
counter = new utils.Counter(3) // each test must start with a fresh couter
})
// test('[6] the FIRST CALL of counter.countDown returns the initial count', () => {})
// test('[7] the SECOND CALL of counter.countDown returns the initial count minus one', () => {})
// test('[8] the count eventually reaches zero but does not go below zero', () => {})
})

describe('[Exercise 5] Seasons', () => {
let seasons
counter = new utils.Counter(3); // each test must start with a fresh counter
});
test("[6] the FIRST CALL of counter.countDown returns the initial count", () => {
expect(counter.countDown()).toBe(3);
});
test("[7] the SECOND CALL of counter.countDown returns the initial count minus one", () => {
counter.countDown();
expect(counter.countDown()).toBe(2);
});
test("[8] the count eventually reaches zero but does not go below zero", () => {
counter.countDown();
counter.countDown();
counter.countDown();
counter.countDown();
expect(counter.countDown()).toBe(0);
});
});

describe("[Exercise 5] Seasons", () => {
let seasons;
beforeEach(() => {
seasons = new utils.Seasons() // each test must start with fresh seasons
})
// test('[9] the FIRST call of seasons.next returns "summer"', () => {})
// test('[10] the SECOND call of seasons.next returns "fall"', () => {})
// test('[11] the THIRD call of seasons.next returns "winter"', () => {})
// test('[12] the FOURTH call of seasons.next returns "spring"', () => {})
// test('[13] the FIFTH call of seasons.next returns again "summer"', () => {})
// test('[14] the 40th call of seasons.next returns "spring"', () => {})
})

describe('[Exercise 6] Car', () => {
let focus
seasons = new utils.Seasons(); // each test must start with fresh seasons
});
test('[9] the FIRST call of seasons.next returns "summer"', () => {
expect(seasons.next()).toBe("summer");
});
test('[10] the SECOND call of seasons.next returns "fall"', () => {
seasons.next();
expect(seasons.next()).toBe("fall");
});
test('[11] the THIRD call of seasons.next returns "winter"', () => {
seasons.next();
seasons.next();
expect(seasons.next()).toBe("winter");
});
test('[12] the FOURTH call of seasons.next returns "spring"', () => {
seasons.next();
seasons.next();
seasons.next();
expect(seasons.next()).toBe("spring");
});
test('[13] the FIFTH call of seasons.next returns again "summer"', () => {
seasons.next();
seasons.next();
seasons.next();
seasons.next();
expect(seasons.next()).toBe("summer");
});
test('[14] the 40th call of seasons.next returns "spring"', () => {
for (let i = 0; i < 39; i++) {
seasons.next();
}
expect(seasons.next()).toBe("spring");
});
});

describe("[Exercise 6] Car", () => {
let focus;
beforeEach(() => {
focus = new utils.Car('focus', 20, 30) // each test must start with a fresh car
})
// test('[15] driving the car returns the updated odometer', () => {})
// test('[16] driving the car uses gas', () => {})
// test('[17] refueling allows to keep driving', () => {})
// test('[18] adding fuel to a full tank has no effect', () => {})
})

describe('[Exercise 7] isEvenNumberAsync', () => {
// test('[19] resolves true if passed an even number', () => {})
// test('[20] resolves false if passed an odd number', () => {})
})
focus = new utils.Car("focus", 20, 30); // each test must start with a fresh car
});
test("[B] has an odometer initialized at zero", () => {
expect(focus.odometer).toBe(0);
});
test("[15] driving the car returns the updated odometer", () => {
expect(focus.drive(100)).toBe(100);
expect(focus.drive(100)).toBe(200);
expect(focus.drive(100)).toBe(300);
expect(focus.drive(200)).toBe(500);
// expect(focus.drive(200)).toBe(600);
});

test("[16] driving the car uses gas", () => {
focus.drive(600);
expect(focus.drive(1)).toBe(600);
expect(focus.drive(1)).toBe(600);
expect(focus.drive(1)).toBe(600);
expect(focus.tank).toBe(0);
});
test("[17] refueling allows to keep driving", () => {
focus.drive(600);
focus.refuel(10);
focus.drive(600);
expect(focus.odometer).toBe(900);
focus.refuel(20);
focus.drive(600);
expect(focus.odometer).toBe(1500);
});

test("[18] adding fuel to a full tank has no effect", () => {
focus.refuel(20000000);
focus.drive(100000);
expect(focus.odometer).toBe(600);
});
});

describe("[Exercise 7] isEvenNumberAsync", () => {
test("[19] resolves true if passed an even number", async () => {
const result = await utils.isEvenNumberAsync(2);
expect(result).toBe(true);
});

test("[20] resolves false if passed an odd number", async () => {
const result = await utils.isEvenNumberAsync(3);
expect(result).toBe(false);
});

test("[21] rejects an error - NOT ON TEST", async () => {
try {
await utils.isEvenNumberAsync("isNaN");
} catch (error) {
expect(error.message).toMatch(/error/i);
}
});
});
2 changes: 1 addition & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ module.exports = {
// bail: 0,

// The directory where Jest should store its cached dependency information
// cacheDirectory: "/private/var/folders/78/wsw4bc497l1b0dlb3f77xh380000gn/T/jest_dx",
// cacheDirectory: "/private/var/folders/4q/91xyqb651fz9_4dwn3l5flyr0000gn/T/jest_dx",

// Automatically clear mock calls and instances between every test
// clearMocks: false,
Expand Down