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

Hossein-w2-JavaScript #10

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
15 changes: 15 additions & 0 deletions .test-summary/TEST_SUMMARY.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
## Test Summary

**Mentors**: For more information on how to review homework assignments, please refer to the [Review Guide](https://github.com/HackYourFuture/mentors/blob/main/assignment-support/review-guide.md).

### 1-JavaScript - Week2

| Exercise | Passed | Failed | ESLint |
|----------------------|--------|--------|--------|
| ex1-giveCompliment | 7 | - | ✕ |
| ex2-dogYears | 7 | - | ✕ |
| ex3-tellFortune | 10 | - | ✕ |
| ex4-shoppingCart | - | - | ✕ |
| ex5-shoppingCartPure | - | - | ✕ |
| ex6-totalCost | - | - | ✕ |
| ex7-mindPrivacy | - | - | ✕ |
39 changes: 26 additions & 13 deletions 1-JavaScript/Week2/assignment/ex1-giveCompliment.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,40 +3,53 @@ Full description at: https://github.com/HackYourFuture/Assignments/tree/main/1-J

1. Complete the function named `giveCompliment`as follows:

- It should take a single parameter: `name`.
- It should take a single parameter: name.
- Its function body should include a variable that holds an array,
`compliments`, initialized with 10 strings. Each string should be a
compliment, like `"great"`, `"awesome"` and so on.
compliments, initialized with 10 strings. Each string should be a
compliment, like "great", "awesome" and so on.
- It should randomly select a compliment from the array.
- It should return the string "You are `compliment`, `name`!", where
`compliment` is a randomly selected compliment and `name` is the name that
- It should return the string "You are compliment, name!", where
compliment is a randomly selected compliment and name is the name that
was passed as an argument to the function.

2. Call the function three times, giving each function call the same argument:
your name.
Use `console.log` each time to display the return value of the
`giveCompliment` function to the console.
Use console.log each time to display the return value of the
giveCompliment function to the console.
-----------------------------------------------------------------------------*/
export function giveCompliment(/* TODO parameter(s) go here */) {
// TODO complete this function
export function giveCompliment(name) {
const compliments = [
'great',
'awesome',
'excellent',
'fantastic',
'wonderful',
'lovely',
'amazing',
'impressive',
'remarkable',
'Good',
];
const randomIndex = Math.floor(Math.random() * compliments.length);
const randomCompliment = compliments[randomIndex];

return `You are ${randomCompliment}, ${name}!`;
}

function main() {
// TODO substitute your own name for "HackYourFuture"
const myName = 'HackYourFuture';
const myName = 'Herbert';

console.log(giveCompliment(myName));
console.log(giveCompliment(myName));
console.log(giveCompliment(myName));

const yourName = 'Amsterdam';
const yourName = 'Mike';

console.log(giveCompliment(yourName));
console.log(giveCompliment(yourName));
console.log(giveCompliment(yourName));
}

// ! Do not change or remove the code below
if (process.env.NODE_ENV !== 'test') {
main();
}
8 changes: 5 additions & 3 deletions 1-JavaScript/Week2/assignment/ex2-dogYears.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@ calculate it!
2. Use `console.log` to display the result of the function for three different
ages.
-----------------------------------------------------------------------------*/
export function calculateDogAge(humanYears) {
// Convert human years to dog years using the rate 1 human year = 7 dog years
const dogYears = humanYears * 7;

export function calculateDogAge(/* TODO parameter(s) go here */) {
// TODO complete this function
// Return the result as a string
return `Your doggie is ${dogYears} years old in dog years!`;
}

function main() {
console.log(calculateDogAge(1)); // -> "Your doggie is 7 years old in dog years!"
console.log(calculateDogAge(2)); // -> "Your doggie is 14 years old in dog years!"
Expand Down
43 changes: 22 additions & 21 deletions 1-JavaScript/Week2/assignment/ex3-tellFortune.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,34 +29,35 @@ Note: The DRY principle is put into practice here: instead of repeating the code
randomly select array elements four times inside the `tellFortune` function
body, this code is now written once only in a separated function.
-----------------------------------------------------------------------------*/
// This function takes an array as its parameter and returns
// a randomly selected element from the array.
function selectRandomly(array) {
// Generate a random index between 0 and the length of the array
const randomIndex = Math.floor(Math.random() * array.length);

// This function should take an array as its parameter and return
// a randomly selected element as its return value.
function selectRandomly(/* TODO parameter(s) go here */) {
// TODO complete this function
// Return the element at the random index
return array[randomIndex];
}

export function tellFortune(/* TODO add parameter(s) here */) {
// TODO complete this function
export function tellFortune(numKids, partnerNames, locations, jobTitles) {
// Use `selectRandomly` to select a random value from each array
const numOfKids = selectRandomly(numKids);
const partnerName = selectRandomly(partnerNames);
const location = selectRandomly(locations);
const jobTitle = selectRandomly(jobTitles);

// Return the formatted string
return `You will be a ${jobTitle} in ${location}, married to ${partnerName} with ${numOfKids} kids.`;
}

function main() {
const numKids = [
// TODO add elements here
];

const partnerNames = [
// TODO add elements here
];

const locations = [
// TODO add elements here
];

const jobTitles = [
// TODO add elements here
];
// Arrays with random values
const numKids = [1, 2, 3, 4, 5];
const partnerNames = ['Alex', 'Jordan', 'Taylor', 'Chris', 'Jamie'];
const locations = ['New York', 'Paris', 'Tokyo', 'Sydney', 'Amsterdam'];
const jobTitles = ['developer', 'teacher', 'designer', 'artist', 'engineer'];

// Call `tellFortune` three times with the arrays as arguments
console.log(tellFortune(numKids, partnerNames, locations, jobTitles));
console.log(tellFortune(numKids, partnerNames, locations, jobTitles));
console.log(tellFortune(numKids, partnerNames, locations, jobTitles));
Expand Down
55 changes: 46 additions & 9 deletions 1-JavaScript/Week2/assignment/ex4-shoppingCart.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,31 @@ you have more than 3 items in your shopping cart the first item gets taken out.
-----------------------------------------------------------------------------*/
const shoppingCart = ['bananas', 'milk'];

// Helper function to convert the shopping cart to a string
function cartToString(cart) {
return cart.join(', ');
}
// ! Function to be tested
function addToShoppingCart(/* parameters go here */) {
// TODO complete this function
function addToShoppingCart(item) {
// Test 1: If no argument is provided, return the cart unchanged
if (arguments.length === 0) {
return `You bought ${cartToString(shoppingCart)}!`;
}

// Test 2: Ensure only one argument is passed
if (arguments.length !== 1) {
throw new Error('addToShoppingCart() accepts exactly one argument');
}

// Add the item to the cart
shoppingCart.push(item);

// If the cart exceeds 3 items, remove the oldest item
if (shoppingCart.length > 3) {
shoppingCart.shift();
}

return `You bought ${cartToString(shoppingCart)}!`;
}

// ! Test functions (plain vanilla JavaScript)
Expand All @@ -29,36 +51,51 @@ function test1() {
'Test 1: addShoppingCart() called without an argument should leave the shopping cart unchanged'
);
const expected = 'You bought bananas, milk!';
const actual = addToShoppingCart();
console.assert(actual === expected);
const actual = addToShoppingCart(); // No argument passed
console.assert(
actual === expected,
`Expected: ${expected}, but got: ${actual}`
);
}

function test2() {
console.log('Test 2: addShoppingCart() should take one parameter');
const expected = 1;
const expected = 1; // The function should only take one parameter
const actual = addToShoppingCart.length;
console.assert(actual === expected);
console.assert(
actual === expected,
`Expected: ${expected}, but got: ${actual}`
);
}

function test3() {
console.log('Test 3: `chocolate` should be added');
const expected = 'You bought bananas, milk, chocolate!';
const actual = addToShoppingCart('chocolate');
console.assert(actual === expected);
console.assert(
actual === expected,
`Expected: ${expected}, but got: ${actual}`
);
}

function test4() {
console.log('Test 4: `waffles` should be added and `bananas` removed');
const expected = 'You bought milk, chocolate, waffles!';
const actual = addToShoppingCart('waffles');
console.assert(actual === expected);
console.assert(
actual === expected,
`Expected: ${expected}, but got: ${actual}`
);
}

function test5() {
console.log('Test 5: `tea` should be added and `milk` removed');
const expected = 'You bought chocolate, waffles, tea!';
const actual = addToShoppingCart('tea');
console.assert(actual === expected);
console.assert(
actual === expected,
`Expected: ${expected}, but got: ${actual}`
);
}

function test() {
Expand Down
9 changes: 7 additions & 2 deletions 1-JavaScript/Week2/assignment/ex5-shoppingCartPure.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,13 @@ it pure. Do the following:
5. Confirm that you function passes the provided unit tests.
------------------------------------------------------------------------------*/
// ! Function under test
function addToShoppingCart(/* TODO parameter(s) go here */) {
// TODO complete this function
function addToShoppingCart(cart, item) {
const newCart = [...cart, item];

if (newCart.length > 3) {
newCart.shift();
}
return newCart;
}

// ! Test functions (plain vanilla JavaScript)
Expand Down
28 changes: 21 additions & 7 deletions 1-JavaScript/Week2/assignment/ex6-totalCost.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,36 @@ instead!
3. Complete the unit test functions and verify that all is working as expected.
-----------------------------------------------------------------------------*/
const cartForParty = {
// TODO complete this object
beers: 1.75,
iceCream: 2.0,
chips: 0.99,
candy: 1.5,
cookies: 1.25,
};

function calculateTotalPrice(/* TODO parameter(s) go here */) {
// TODO replace this comment with your code
function calculateTotalPrice(cart) {
const total = Object.values(cart).reduce((sum, price) => sum + price, 0);

return `Total: €${total.toFixed(2)}`;
}

// ! Test functions (plain vanilla JavaScript)
function test1() {
console.log('\nTest 1: calculateTotalPrice should take one parameter');
// TODO replace this comment with your code
console.log('Test 1: calculateTotalPrice should take one parameter');
console.assert(
calculateTotalPrice.length === 1,
'Expected function to take one parameter'
);
}

function test2() {
console.log('\nTest 2: return correct output when passed cartForParty');
// TODO replace this comment with your code
console.log('Test 2: return correct output when passed cartForParty');
const expected = 'Total: €7.49';
const actual = calculateTotalPrice(cartForParty);
console.assert(
actual === expected,
`Expected: ${expected}, but got: ${actual}`
);
}

function test() {
Expand Down
8 changes: 6 additions & 2 deletions 1-JavaScript/Week2/assignment/ex7-mindPrivacy.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,12 @@ const employeeRecords = [
];

// ! Function under test
function filterPrivateData(/* TODO parameter(s) go here */) {
// TODO complete this function
function filterPrivateData(employeeRecords) {
return employeeRecords.map(({ name, occupation, email }) => ({
name,
occupation,
email,
}));
}

// ! Test functions (plain vanilla JavaScript)
Expand Down
19 changes: 19 additions & 0 deletions 1-JavaScript/Week2/test-reports/ex1-giveCompliment.report.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
*** Unit Test Error Report ***

PASS .dist/1-JavaScript/Week2/unit-tests/ex1-giveCompliment.test.js
js-wk2-ex1-giveCompliment
✅ should exist and be executable (3 ms)
✅ should have all TODO comments removed
✅ `giveCompliment` should not contain unneeded console.log calls (1 ms)
✅ should take a single parameter (1 ms)
✅ should include a `compliments` array inside its function body (1 ms)
✅ the `compliments` array should be initialized with 10 strings
✅ should give a random compliment: You are `compliment`, `name`! (1 ms)

Test Suites: 1 passed, 1 total
Tests: 7 passed, 7 total
Snapshots: 0 total
Time: 2.6 s
Ran all test suites matching /\/Users\/hackyourfuture\/Desktop\/Assignments-cohort51\/.dist\/1-JavaScript\/Week2\/unit-tests\/ex1-giveCompliment.test.js/i.
No linting errors detected.
No spelling errors detected.
19 changes: 19 additions & 0 deletions 1-JavaScript/Week2/test-reports/ex2-dogYears.report.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
*** Unit Test Error Report ***

PASS .dist/1-JavaScript/Week2/unit-tests/ex2-dogYears.test.js
js-wk2-ex2-dogYears
✅ should exist and be executable (3 ms)
✅ should have all TODO comments removed (1 ms)
✅ `calculateDogAge` should not contain unneeded console.log calls (2 ms)
✅ should take a single parameter (1 ms)
✅ should give 7 dog years for 1 human year (1 ms)
✅ should give 14 dog years for 2 human years (1 ms)
✅ give 21 dog years for 3 human years

Test Suites: 1 passed, 1 total
Tests: 7 passed, 7 total
Snapshots: 0 total
Time: 0.904 s
Ran all test suites matching /\/Users\/hackyourfuture\/Desktop\/Assignments-cohort51\/.dist\/1-JavaScript\/Week2\/unit-tests\/ex2-dogYears.test.js/i.
No linting errors detected.
No spelling errors detected.
22 changes: 22 additions & 0 deletions 1-JavaScript/Week2/test-reports/ex3-tellFortune.report.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
*** Unit Test Error Report ***

PASS .dist/1-JavaScript/Week2/unit-tests/ex3-tellFortune.test.js
js-wk2-ex3-tellFortune
✅ should exist and be executable (3 ms)
✅ should have all TODO comments removed (1 ms)
✅ `tellFortune` should not contain unneeded console.log calls (2 ms)
✅ should take four parameters (1 ms)
✅ should call function `selectRandomly` for each of its arguments (2 ms)
✅ `numKids` should be an array initialized with 5 elements (1 ms)
✅ `locations` should be an array initialized with 5 elements (1 ms)
✅ `partnerNames` should be an array initialized with 5 elements
✅ `jobTitles` should be an array initialized with 5 elements (1 ms)
✅ should tell the fortune by randomly selecting array values (1 ms)

Test Suites: 1 passed, 1 total
Tests: 10 passed, 10 total
Snapshots: 0 total
Time: 0.933 s
Ran all test suites matching /\/Users\/hackyourfuture\/Desktop\/Assignments-cohort51\/.dist\/1-JavaScript\/Week2\/unit-tests\/ex3-tellFortune.test.js/i.
No linting errors detected.
No spelling errors detected.
3 changes: 3 additions & 0 deletions 1-JavaScript/Week2/test-reports/ex4-shoppingCart.report.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
A unit test file was not provided for this exercise.
No linting errors detected.
No spelling errors detected.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
A unit test file was not provided for this exercise.
No linting errors detected.
No spelling errors detected.
3 changes: 3 additions & 0 deletions 1-JavaScript/Week2/test-reports/ex6-totalCost.report.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
A unit test file was not provided for this exercise.
No linting errors detected.
No spelling errors detected.
Loading