Skip to content

initial #28

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 7 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
23 changes: 11 additions & 12 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
{
"env": {
"commonjs": true,
"es2021": true,
"node": true,
"jest": true
},
"extends": "eslint:recommended",
"parserOptions": {
"ecmaVersion": 12
},
"rules": {
}
"env": {
"commonjs": true,
"es2021": true,
"node": true,
"jest": true
},
"extends": "eslint:recommended",
"parserOptions": {
"ecmaVersion": 12
},
"rules": {}
}
Binary file added 2021-07-21-17-19-02.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
48 changes: 46 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,15 @@ There are two possible ways to submit your project. Your instructor should have
### Task 2: Minimum Viable Product

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

#### Notes

- Run `index.js` with Nodemon executing `npm run dev`.
- Run tests locally with Jest executing `npm test`.
- You can add console.logs to `index.js` to manually test your code. (e.g. `console.log(car.drive(10));`).
- The output of your log statements can be found in the terminal you run `npm run dev` in.
- You must remove the `.todo` from the tests in order for them to execute.

#### Hot Tips

Expand All @@ -44,3 +43,48 @@ There are two possible ways to submit your project. Your instructor should have
- In your solution, it is essential that you follow best practices and produce clean and professional results.
- Schedule time to review, refine, and assess your work.
- Perform basic professional polishing including spell-checking and grammar-checking on your work.

# Result:

```

PASS ./index.test.js (16.312 s)
[Exercise 1] trimProperties
✓ [1] returns an object with the properties trimmed (7 ms)
✓ [2] returns a copy, leaving the original object intact (1 ms)
[Exercise 2] trimPropertiesMutation
✓ [3] returns an object with the properties trimmed (1 ms)
✓ [4] the object returned is the exact same one we passed in (1 ms)
[Exercise 3] findLargestInteger
✓ [5] returns the largest number in an array of objects { integer: 2 }
[Exercise 4] Counter
✓ [6] the FIRST CALL of counter.countDown returns the initial count (1 ms)
✓ [7] the SECOND CALL of counter.countDown returns the initial count minus one
✓ [8] the count eventually reaches zero but does not go below zero (1 ms)
[Exercise 5] Seasons
✓ [9] the FIRST call of seasons.next returns "summer" (1 ms)
✓ [10] the SECOND call of seasons.next returns "fall"
✓ [11] the THIRD call of seasons.next returns "winter" (1 ms)
✓ [12] the FOURTH call of seasons.next returns "spring"
✓ [13] the FIFTH call of seasons.next returns again "summer"
✓ [14] the 40th call of seasons.next returns "spring"
[Exercise 6] Car
✓ [15] driving the car returns the updated odometer (1 ms)
✓ [16] driving the car uses gas
✓ [17] refueling allows to keep driving (1 ms)
✓ [18] adding fuel to a full tank has no effect
[Exercise 7] isEvenNumberAsync
✓ [19] resolves true if passed an even number (1 ms)
✓ [20] resolves false if passed an odd number

Test Suites: 1 passed, 1 total
Tests: 20 passed, 20 total
Snapshots: 0 total
Time: 17.693 s
Ran all test suites.



```

![](2021-07-21-17-19-02.png)
65 changes: 51 additions & 14 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@
*/
function trimProperties(obj) {
// ✨ implement
const trimmed = {};
for (const prop in obj) {
trimmed[prop] = obj[prop].trim();
}
return trimmed;
}

/**
Expand All @@ -19,6 +24,10 @@ function trimProperties(obj) {
* trimPropertiesMutation({ name: ' jane ' }) // returns the object mutated in place { name: 'jane' }
*/
function trimPropertiesMutation(obj) {
for (const prop in obj) {
obj[prop] = obj[prop].trim();
}
return obj;
// ✨ implement
}

Expand All @@ -32,6 +41,7 @@ function trimPropertiesMutation(obj) {
*/
function findLargestInteger(integers) {
// ✨ implement
return Math.max(...integers);
}

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

/**
Expand All @@ -56,7 +68,11 @@ class Counter {
* counter.countDown() // returns 0
*/
countDown() {
// ✨ implement
if (!this.initialized) {
this.initialized = true;
return this.count;
}
return this.count > 0 ? --this.count : this.count;
}
}

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

/**
Expand All @@ -82,6 +100,12 @@ class Seasons {
*/
next() {
// ✨ implement
if (this.season === "spring") {
this.season = this.seasons[0];
} else {
this.season = this.seasons[this.seasons.indexOf(this.season) + 1];
}
return this.season;
}
}

Expand All @@ -93,14 +117,17 @@ 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.name = name;
this.mpg = mpg;
}

/**
* [Exercise 6B] Car.prototype.drive adds miles to the odometer and consumes fuel according to mpg
* @param {string} distance - the distance we want the car to drive
* @param {number} distance - the distance we want the car to drive
* @returns {number} - the updated odometer value
*
* EXAMPLE
Expand All @@ -112,7 +139,16 @@ class Car {
* focus.drive(200) // returns 600 (ran out of gas after 100 miles)
*/
drive(distance) {
// ✨ implement
while (distance !== 0) {
if (0 <= this.tank * this.mpg) {
distance -= 1;
this.odometer += 1;
this.tank -= 1 / this.mpg;
} else {
distance = 0;
}
}
return this.odometer;
}

/**
Expand All @@ -127,7 +163,8 @@ class Car {
* focus.refuel(99) // returns 600 (tank only holds 20)
*/
refuel(gallons) {
// ✨ implement
this.tank = clamp(this.tank + gallons, 0, this.tankSize);
return this.mpg * this.tank;
}
}

Expand All @@ -143,17 +180,17 @@ class Car {
* isEvenNumberAsync(3).then(result => {
* // result is false
* })
* isEvenNumberAsync('foo').catch(error => {
* // error.message is "number must be a number"
* })
* isEvenNumberAsync(NaN).catch(error => {
* // error.message is "number must be a number"
* })
*/
function isEvenNumberAsync(number) {
// ✨ implement
return new Promise((res) => {
res(number % 2 === 0 ? true : false);
});
}

const clamp = (num, min, max) => {
return Math.min(Math.max(num, min), max);
};

module.exports = {
trimProperties,
trimPropertiesMutation,
Expand All @@ -162,4 +199,4 @@ module.exports = {
Counter,
Seasons,
Car,
}
};
Loading