Skip to content
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
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,23 @@ There are two possible ways to submit your project. Your instructor should have
#### Option A - Codegrade

- [ ] Fork and clone the repository.
- [ ] Open the assignment in Canvas and click on the "Set up git" option.
- [] Open the assignment in Canvas and click on the "Set up git" option.
- [ ] Follow instructions to set up Codegrade's Webhook and Deploy Key.
- [ ] Push your first commit: `git commit --allow-empty -m "first commit" && git push`.
- [ ] Check to see that Codegrade has accepted your git submission.

#### Option B - Pull Request

- [ ] Fork and clone the repository.
- [ ] Implement your project in a `firstname-lastname` branch.
- [ ] Create a pull request of `firstname-lastname` against your `main` branch.
- [ ] Open the assignment in Canvas and submit your pull request.
- [x ] Fork and clone the repository.
- [ x] Implement your project in a `firstname-lastname` branch.
- [ x] Create a pull request of `firstname-lastname` against your `main` branch.
- [ x] Open the assignment in Canvas and submit your pull request.

### Task 2: Minimum Viable Product

- [ ] For Exercises 1-7 inside `index.js`:
- [ ] Implement the function or the class in `index.js`.
- [ ] Write the corresponding tests in `index.test.js`.
- [ x] For Exercises 1-7 inside `index.js`:
- [x ] Implement the function or the class in `index.js`.
- [x ] Write the corresponding tests in `index.test.js`.

#### Notes

Expand Down
78 changes: 67 additions & 11 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,20 @@
* trimProperties({ name: ' jane ' }) // returns a new object { name: 'jane' }
*/
function trimProperties(obj) {
// ✨ implement
const objCopy = {};

for (const key in obj) {
const value = obj[key];

if(typeof value === 'string') {
const trimmedValue = value.trim();
objCopy[key] = trimmedValue;
} else {
objCopy[key] = value;
}

};
return objCopy;
}

/**
Expand All @@ -19,7 +32,15 @@ function trimProperties(obj) {
* trimPropertiesMutation({ name: ' jane ' }) // returns the object mutated in place { name: 'jane' }
*/
function trimPropertiesMutation(obj) {
// ✨ implement
for (const key in obj) {
const value = obj[key];

if(typeof value === "string") {
const trimmedValue = value.trim();
obj[key] = trimmedValue;
}
};
return obj.
}

/**
Expand All @@ -31,7 +52,15 @@ function trimPropertiesMutation(obj) {
* findLargestInteger([{ integer: 1 }, { integer: 3 }, { integer: 2 }]) // returns 3
*/
function findLargestInteger(integers) {
// ✨ implement
let largestInteger = 0;

integers.forEach(integer => {
if (integer.integer > largestInteger) {
largestInteger = integer.integer;
}

})
return largestInteger;
}

class Counter {
Expand All @@ -40,7 +69,7 @@ class Counter {
* @param {number} initialNumber - the initial state of the count
*/
constructor(initialNumber) {
// ✨ initialize whatever properties are needed
this.number = initialNumber;
}

/**
Expand All @@ -56,7 +85,11 @@ class Counter {
* counter.countDown() // returns 0
*/
countDown() {
// ✨ implement
const originalNumber = this.number;
if(this.number > 0){
this.number -= 1; //becuse we're counting down
}
return originalNumber;
}
}

Expand All @@ -65,7 +98,7 @@ class Seasons {
* [Exercise 5A] Seasons creates a seasons object
*/
constructor() {
// ✨ initialize whatever properties are needed
this.season = initialSeason;
}

/**
Expand All @@ -81,7 +114,15 @@ class Seasons {
* seasons.next() // returns "summer"
*/
next() {
// ✨ implement
const originalSeason = this.season;

const seasons = ["summer, fall, winter, spring"];
const originalIndex = seasons.indexOf(orignalSeason);
const nextIndex = (originalIndex + 1) % seasons.length; //Is there a simpler way to achieve this?

const nextSeason = seasons[nextIndex];
this.season = nextSeason;
return originalSeason;
}
}

Expand All @@ -95,7 +136,7 @@ class Car {
constructor(name, tankSize, mpg) {
this.odometer = 0 // car initilizes with zero miles
this.tank = tankSize // car initiazes full of gas
// ✨ initialize whatever other properties are needed
this.mpg = mpg
}

/**
Expand All @@ -112,7 +153,13 @@ class Car {
* focus.drive(200) // returns 600 (ran out of gas after 100 miles)
*/
drive(distance) {
// ✨ implement
const originalOdometer = this.odometer;
if (this.tank > 0){
this.tank -= distance /this.mpg;
this.odometer += distance;

}
return this.odometer;
}

/**
Expand All @@ -127,7 +174,8 @@ class Car {
* focus.refuel(99) // returns 600 (tank only holds 20)
*/
refuel(gallons) {
// ✨ implement
const gallons = this.gallons;
if (this.odometer)
}
}

Expand All @@ -151,8 +199,16 @@ class Car {
* })
*/
function isEvenNumberAsync(number) {
// ✨ implement

}
//ASYNC STRUCTURE
// it("name", async () => {
// const distance = await odyssey.driveAsync(10)
// expect(distance).toBe(10)
// })




module.exports = {
trimProperties,
Expand Down
86 changes: 80 additions & 6 deletions index.test.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,56 @@
const utils = require('./index')
const {
trimProperties,
trimPropertiesMutation,
findLargestInteger,
Counter,
Seasons,
Car
} = require ('./index');


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', () => {})
const input = { foo: ' foo ', bar: 'bar ', baz: ' baz' }//arrange
const expected = { foo: 'foo', bar: 'bar', baz: 'baz' }//arrange
const actual = utils.trimProperties(input)//act
expect(actual).toEqual(expected) //assert
})

test('[2] returns a copy, leaving the original object intact', () => {
const input = {name: ' jane ', age: ' 20 '}
let expected = {name: 'jane', age:'20'}
expect(trimProperties(input)).toEqual(expected)
expect(trimProperties(input)).not.toBe(expected)
})

})

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', () => {})
const input = { name: ' jane ', age: ' 20 '}
let expected = { name: 'jane', age: '20' }

test('[3] returns an object with the properties trimmed', () => {
expect(trimPropertiesMutation(input)).toEqual(expected)
})

test('[4] the object returned is the exact same one we passed in', () => {
expect(trimPropertiesMutation(input)).toBe(input)
})

})

})

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

})

describe('[Exercise 4] Counter', () => {
Expand All @@ -28,6 +61,18 @@ describe('[Exercise 4] Counter', () => {
// 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', () => {})
test('[6] the FIRST CALL of counter.countDown returns the initial count', () => {
expect(counter.countDown()).toEqual(3)
})

test('[7] the SECOND CALL of counter.countDown returns the initial count minus one', () => {
expect(counter.countDown()).not.toEqual(2)
})

test('[8] the count eventually reaches zero but does not go below zero', () => {
expect(counter.countDown()).toBeGreaterThanOrEqual(0)
})

})

describe('[Exercise 5] Seasons', () => {
Expand All @@ -41,6 +86,32 @@ describe('[Exercise 5] Seasons', () => {
// 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"', () => {})

test('[9] the FIRST call of seasons.next returns "summer"', () => {
expect(seasons.next()).toEqual('summer')
})
test('[10] the SECOND call of seasons.next returns "fall"', () => {
expect(seasons.next()).toEqual('fall')
})
test('[11] the THIRD call of seasons.next returns "winter"', () => {
expect(seasons.next()).toEqual('winter')
})
test('[12] the FOURTH call of seasons.next returns "spring"', () => {
expect(seasons.next()).toEqual('spring')
})
test('[13] the FIFTH call of seasons.next returns again "summer"', () => {
expect(seasons.next()).toEqual('summer')
})
test('[14] the 40th call of seasons.next returns "spring"', () => {
// create loop 35 times to keep calling next until it returns spring
let testSeason = ""

for (let i = 5; i < 40; i++) {
testSeason = seasons.next()
}
expect(testSeason).toEqual('spring')

})
})

describe('[Exercise 6] Car', () => {
Expand All @@ -52,9 +123,12 @@ describe('[Exercise 6] Car', () => {
// 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', () => {})
})
//EXAMPLE
//return Promise.resolve(distance) -- how you return the asychronous... stuff