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

Tony miller MVP #24

Open
wants to merge 4 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
83 changes: 77 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,15 @@
*/
function trimProperties(obj) {
// ✨ implement
var trimmedObj = {};
for (var prop in obj) {
if (typeof obj[prop] === "string") {
trimmedObj[prop] = obj[prop].trim();
} else {
trimmedObj[prop] = obj[prop];
}
}
return trimmedObj;
}

/**
Expand All @@ -20,6 +29,12 @@ function trimProperties(obj) {
*/
function trimPropertiesMutation(obj) {
// ✨ implement
for (var prop in obj) {
if (typeof obj[prop] === "string") {
obj[prop] = obj[prop].trim();
}
}
return obj;
}

/**
Expand All @@ -32,6 +47,13 @@ function trimPropertiesMutation(obj) {
*/
function findLargestInteger(integers) {
// ✨ implement
var largest = 0;
for (var i = 0; i < integers.length; i++) {
if (integers[i].integer > largest) {
largest = integers[i].integer;
}
}
return largest;
}

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

/**
Expand All @@ -57,15 +79,19 @@ class Counter {
*/
countDown() {
// ✨ implement
this.count = this.count - 1;
return this.count > 0 ? this.count : 0;
}
}

class Seasons {
/**
* [Exercise 5A] Seasons creates a seasons object
*/
constructor() {
constructor(initialSeason) {
// ✨ initialize whatever properties are needed
this.season = initialSeason;
this.currentSeason = 'spring'
}

/**
Expand All @@ -82,6 +108,21 @@ class Seasons {
*/
next() {
// ✨ implement
const season = this.currentSeason;
if (season === 'spring') {
this.currentSeason = 'summer'
}
if (season === 'summer') {
this.currentSeason = 'fall'
}
if (season === 'fall') {
this.currentSeason = 'winter'
}
if (season === 'winter') {
this.currentSeason = 'spring'
}
return this.currentSeason;

}
}

Expand All @@ -93,8 +134,10 @@ 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 initializes with zero miles
this.tank = tankSize; // car initializes full of gas
this.mpg = mpg; // car initializes with a constant mpg
this.tankCapacity = tankSize; // car initializes with a name
// ✨ initialize whatever other properties are needed
}

Expand All @@ -113,6 +156,19 @@ class Car {
*/
drive(distance) {
// ✨ implement
const currentRange = this.tank * this.mpg
const fuelRequired = distance / this.mpg

if (distance > currentRange) {
this.odometer += currentRange
this.tank = 0
}
else {
this.odometer += distance
this.tank -= fuelRequired
}

return Car
}

/**
Expand All @@ -128,6 +184,12 @@ class Car {
*/
refuel(gallons) {
// ✨ implement
if (this.tank + gallons > this.tankCapacity ){
this.tank = this.tankCapacity
return Car
}
this.tank += gallons
return Car
}
}

Expand All @@ -150,8 +212,17 @@ class Car {
* // error.message is "number must be a number"
* })
*/
function isEvenNumberAsync(number) {
async function isEvenNumberAsync(number) {
// ✨ implement
if(!number || typeof number !== "number"){
return false
}

if(number % 2 === 0){
return true
}

return false
}

module.exports = {
Expand All @@ -162,4 +233,4 @@ module.exports = {
Counter,
Seasons,
Car,
}
};
149 changes: 127 additions & 22 deletions index.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const utils = require('./index')


describe('[Exercise 1] trimProperties', () => {
test('[1] returns an object with the properties trimmed', () => {
// EXAMPLE
Expand All @@ -8,55 +9,159 @@ describe('[Exercise 1] trimProperties', () => {
const actual = utils.trimProperties(input)
expect(actual).toEqual(expected)
})
test.todo('[2] returns a copy, leaving the original object intact')

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

})

describe('[Exercise 2] trimPropertiesMutation', () => {
test.todo('[3] returns an object with the properties trimmed')
test.todo('[4] the object returned is the exact same one we passed in')
const input = { name: 'Jake', age: '30'}
let expected = { name: 'Jake', age: '30' }

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

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

})


describe('[Exercise 3] findLargestInteger', () => {
test.todo('[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(utils.findLargestInteger([{integer: 100}, {integer: 233}, {integer: 4}])).toBe(233)
})
})

describe('[Exercise 4] Counter', () => {
let counter
beforeEach(() => {
counter = new utils.Counter(3) // each test must start with a fresh couter
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()).toEqual(3)
})

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

test('[8] the count eventually reaches zero but does not go below zero', () => {
for(let i = 0; i < 10; i++) {
counter.countDown()
}

expect(counter.countDown()).toBeGreaterThanOrEqual(0)
})
test.todo('[6] the FIRST CALL of counter.countDown returns the initial count')
test.todo('[7] the SECOND CALL of counter.countDown returns the initial count minus one')
test.todo('[8] the count eventually reaches zero but does not go below zero')
})

describe('[Exercise 5] Seasons', () => {
let seasons
beforeEach(() => {
seasons = new utils.Seasons() // each test must start with fresh seasons
})
test.todo('[9] the FIRST call of seasons.next returns "summer"')
test.todo('[10] the SECOND call of seasons.next returns "fall"')
test.todo('[11] the THIRD call of seasons.next returns "winter"')
test.todo('[12] the FOURTH call of seasons.next returns "spring"')
test.todo('[13] the FIFTH call of seasons.next returns again "summer"')
test.todo('[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"', () => {
for (let i = 0; i < 1; i++) {
seasons.next()
}
expect(seasons.next()).toEqual('fall')
})

test('[11] the THIRD call of seasons.next returns "winter"', () => {
for (let i = 0; i < 2; i++) {
seasons.next()
}
expect(seasons.next()).toEqual('winter')
})

test('[12] the FOURTH call of seasons.next returns "spring"', () => {
for (let i = 0; i < 3; i++) {
seasons.next()
}
expect(seasons.next()).toEqual('spring')
})
test('[13] the FIFTH call of seasons.next returns again "summer"', () => {
for (let i = 0; i < 4; i++) {
seasons.next()
}
expect(seasons.next()).toEqual('summer')
})

test('[14] the 40th call of seasons.next returns "spring"', () => {
for (let i = 0; i < 39; i++) {
seasons.next()
}
expect(seasons.next()).toEqual('spring')
})
})

describe('[Exercise 6] Car', () => {
let focus
beforeEach(() => {
focus = new utils.Car('focus', 20, 30) // each test must start with a fresh car
})
test.todo('[15] driving the car returns the updated odometer')
test.todo('[16] driving the car uses gas')
test.todo('[17] refueling allows to keep driving')
test.todo('[18] adding fuel to a full tank has no effect')
test('[15] driving the car returns the updated odometer', () => {
// ✨ test away
focus.drive(20)
expect(focus.odometer).toBe(20)
})
test('[16] driving the car uses gas', () => {
// ✨ test away
focus.drive(30)
//Miles per gallon 30 miles uses 1 gallon
//Tank started at 20 should be 19 gallons left
expect(focus.tank).toBe(19)
})
test('[17] refueling allows to keep driving', () => {
// ✨ test away
focus.drive(700)
expect(focus.odometer).toBe(600)
focus.refuel(20)
focus.drive(100)
expect(focus.odometer).toBe(700)
})
test('[18] adding fuel to a full tank has no effect', () => {
// ✨ test away
focus.refuel(10)
expect(focus.tank).toBe(20)
focus.drive(600)
focus.refuel(30)
expect(focus.tank).toBe(20)
})
})

describe('[Exercise 7] isEvenNumberAsync', () => {
test.todo('[19] resolves true if passed an even number')
test.todo('[20] resolves false if passed an odd number')
test.todo('[21] rejects an error with the message "number must be a number" if passed a non-number type')
test.todo('[22] rejects an error with the message "number must be a number" if passed NaN')
test('[19] resolves true if passed an even number', async() => {
// ✨ test away
const num = await utils.isEvenNumberAsync(2)
expect(num).toBe(true)
})
test('[20] resolves false if passed an odd number', async () => {
// ✨ test away
const num = await utils.isEvenNumberAsync(1)
expect(num).toBe(false)
})
test('[21] rejects an error with the message "number must be a number" if passed a non-number type', async () => {
// ✨ test away
const num = await utils.isEvenNumberAsync("momma")
expect(num).toBe(false)
})
test('[22] rejects an error with the message "number must be a number" if passed NaN', async () => {
// ✨ test away
const num = await utils.isEvenNumberAsync(NaN)
expect(num).toBe(false)
})
})
Loading