Skip to content

WIP project. Tests and index.js complete except last 2. Will comple… #20

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

Open
wants to merge 1 commit 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
Empty file added NOTES.md
Empty file.
129 changes: 117 additions & 12 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,56 @@
* trimProperties({ name: ' jane ' }) // returns a new object { name: 'jane' }
*/
function trimProperties(obj) {
// ✨ implement
const objCopy = {};

// loop through the values in an object
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;
}

// const test = trimProperties({ name: ' jane ', age: 23 });
// console.log("test", test);

/**
* [Exercise 2] trimPropertiesMutation trims in place the properties of an object
* [Exercise 2] trimPropertiesMutation mutates an object trimming its properties
* @param {object} obj - an object with properties that are strings
* @returns {object} - the same object with strings trimmed
*
* EXAMPLE
* trimPropertiesMutation({ name: ' jane ' }) // returns the object mutated in place { name: 'jane' }
*/
function trimPropertiesMutation(obj) {
// ✨ implement
// loop through the values in an object
for (const key in obj) {

const value = obj[key];

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

};

return obj;
}

// const test = trimPropertiesMutation({ name: ' jane ', age: 23 });
// console.log("test", test);


/**
* [Exercise 3] findLargestInteger finds the largest integer in an array of objects { integer: 1 }
* @param {object[]} integers - an array of objects
Expand All @@ -31,16 +66,29 @@ function trimPropertiesMutation(obj) {
* findLargestInteger([{ integer: 1 }, { integer: 3 }, { integer: 2 }]) // returns 3
*/
function findLargestInteger(integers) {
// ✨ implement
// find the largest integer in an array of objects
let largestInteger = 0;

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

return largestInteger;

}

// const test = findLargestInteger([{ integer: 1 }, { integer: 3 }, { integer: 2 }]);
// console.log("test", test);

class Counter {
/**
* [Exercise 4A] Counter creates a counter
* @param {number} initialNumber - the initial state of the count
*/
constructor(initialNumber) {
// ✨ initialize whatever properties are needed
this.number = initialNumber; // counter initializes with an initial number
}

/**
Expand All @@ -56,16 +104,31 @@ class Counter {
* counter.countDown() // returns 0
*/
countDown() {
// ✨ implement
const originalNumber = this.number;

// zero never goes through the decrement
if (this.number > 0) {
this.number -= 1;
}

return originalNumber;
}
}

// const counter = new Counter(3)
// const counterTwo = new Counter(5)

// console.log("test", counter.countDown());
// console.log("test", counter.countDown());
// console.log("test", counterTwo.countDown());


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

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

// array of seasons
const seasons = ["summer", "fall", "winter", "spring"];
const originalIndex = seasons.indexOf(originalSeason); // gets the index of the original season
const nextIndex = (originalIndex + 1) % seasons.length; // gets the index of the next season

const nextSeason = seasons[nextIndex]; // gets the next season by nextIndex

this.season = nextSeason;
return originalSeason;
}
}

// TEST
const seasonsList = new Seasons("summer")
console.log("test", seasonsList.next());
console.log("test", seasonsList.next());
console.log("test", seasonsList.next());
console.log("test", seasonsList.next());
console.log("test", seasonsList.next());


class Car {
/**
* [Exercise 6A] Car creates a car object
Expand All @@ -95,7 +177,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 // car initiazes with a mpg of miles per gallon
}

/**
Expand All @@ -112,9 +194,19 @@ class Car {
* focus.drive(200) // returns 600 (ran out of gas after 100 miles)
*/
drive(distance) {
// ✨ implement
const originalOdometer = this.odometer;

// checks if car has enough gas
if (this.tank > 0) {
this.tank -= distance / this.mpg;
this.odometer += distance;
}

return this.odometer;
}



/**
* [Exercise 6C] Adds gallons to the tank
* @param {number} gallons - the gallons of fuel we want to put in the tank
Expand All @@ -131,6 +223,16 @@ class Car {
}
}

// TESTa
const focus = new Car('focus', 20, 30)

console.log("test", focus.drive(100));
console.log("test", focus.drive(100));
console.log("test", focus.drive(100));
console.log("test", focus.drive(200))
console.log("test", focus.drive(200))


/**
* [Exercise 7] Asynchronously resolves whether a number is even
* @param {number} number - the number to test for evenness
Expand All @@ -151,7 +253,10 @@ class Car {
* })
*/
function isEvenNumberAsync(number) {
// ✨ implement
// check if numer is even
// return a promise


}

module.exports = {
Expand Down
89 changes: 72 additions & 17 deletions index.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
const utils = require('./index')

const {
trimProperties,
trimPropertiesMutation,
findLargestInteger,
isEvenNumberAsync,
Counter,
Seasons,
Car,
} = require('./index');
//arrange
describe('[Exercise 1] trimProperties', () => {
test('[1] returns an object with the properties trimmed', () => {
// EXAMPLE
Expand All @@ -8,39 +17,85 @@ describe('[Exercise 1] trimProperties', () => {
const actual = utils.trimProperties(input)
expect(actual).toEqual(expected)
})
test.todo('[2] returns a copy, leaving the original object intact')
//act
test('[2] returns a copy, leaving the original object intact', () => {
//assert
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.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: ' 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.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(findLargestInteger([{integer: 2}, {integer: 1}, {integer: 3}])).toEqual(3)
})
})

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