Skip to content

Commit 0a36dd8

Browse files
committed
Add the answer code to the assignment
1 parent 2cbd8e5 commit 0a36dd8

File tree

7 files changed

+106
-28
lines changed

7 files changed

+106
-28
lines changed

1-JavaScript/Week2/assignment/ex1-giveCompliment.js

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,26 +17,42 @@ Full description at: https://github.com/HackYourFuture/Assignments/tree/main/1-J
1717
Use `console.log` each time to display the return value of the
1818
`giveCompliment` function to the console.
1919
-----------------------------------------------------------------------------*/
20-
export function giveCompliment(/* TODO parameter(s) go here */) {
20+
export function giveCompliment(name) {
2121
// TODO complete this function
22+
const compliments = [
23+
"kind",
24+
"smart",
25+
"beautiful",
26+
"strong",
27+
"brave",
28+
"talented",
29+
"generous",
30+
"helpful",
31+
"creative",
32+
"friendly",
33+
];
34+
35+
return `${name} you are ${
36+
compliments[Math.floor(Math.random() * compliments.length)]
37+
}`;
2238
}
2339

2440
function main() {
2541
// TODO substitute your own name for "HackYourFuture"
26-
const myName = 'HackYourFuture';
42+
const myName = "Alaa";
2743

2844
console.log(giveCompliment(myName));
2945
console.log(giveCompliment(myName));
3046
console.log(giveCompliment(myName));
3147

32-
const yourName = 'Amsterdam';
48+
const yourName = "Amsterdam";
3349

3450
console.log(giveCompliment(yourName));
3551
console.log(giveCompliment(yourName));
3652
console.log(giveCompliment(yourName));
3753
}
3854

3955
// ! Do not change or remove the code below
40-
if (process.env.NODE_ENV !== 'test') {
56+
if (process.env.NODE_ENV !== "test") {
4157
main();
4258
}

1-JavaScript/Week2/assignment/ex2-dogYears.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,16 @@ calculate it!
99
- It takes one parameter: your (fictional) puppy's age (number).
1010
- Calculate your dog's age based on the conversion rate of 1 human year to
1111
7 dog years.
12+
// 1 human year = 7 dog years
1213
- Return a string: "Your doggie is `age` years old in dog years!"
1314
1415
2. Use `console.log` to display the result of the function for three different
1516
ages.
1617
-----------------------------------------------------------------------------*/
1718

18-
export function calculateDogAge(/* TODO parameter(s) go here */) {
19+
export function calculateDogAge(age /* TODO parameter(s) go here */) {
1920
// TODO complete this function
21+
return `Your doggie is ${age * 7} years old in dog years!`;
2022
}
2123

2224
function main() {
@@ -26,6 +28,6 @@ function main() {
2628
}
2729

2830
// ! Do not change or remove the code below
29-
if (process.env.NODE_ENV !== 'test') {
31+
if (process.env.NODE_ENV !== "test") {
3032
main();
3133
}

1-JavaScript/Week2/assignment/ex3-tellFortune.js

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,29 +32,57 @@ body, this code is now written once only in a separated function.
3232

3333
// This function should take an array as its parameter and return
3434
// a randomly selected element as its return value.
35-
function selectRandomly(/* TODO parameter(s) go here */) {
35+
function selectRandomly(arr /* TODO parameter(s) go here */) {
3636
// TODO complete this function
37+
return arr[Math.floor(Math.random() * arr.length)];
3738
}
3839

39-
export function tellFortune(/* TODO add parameter(s) here */) {
40+
export function tellFortune(
41+
childrenArr,
42+
partnersArr,
43+
locationsArr,
44+
jobsArr /* TODO add parameter(s) here */
45+
) {
4046
// TODO complete this function
47+
const numKids = selectRandomly(childrenArr);
48+
const partnerName = selectRandomly(partnersArr);
49+
const location = selectRandomly(locationsArr);
50+
const jobTitle = selectRandomly(jobsArr);
51+
52+
return `You will be a ${jobTitle} in ${location}, married to ${partnerName} with ${numKids} kids.`;
4153
}
4254

4355
function main() {
4456
const numKids = [
4557
// TODO add elements here
58+
2, 4, 1, 7, 5,
4659
];
4760

4861
const partnerNames = [
4962
// TODO add elements here
63+
"Yusuf",
64+
"Leen",
65+
"Sarah",
66+
"Eliana",
67+
"Amal",
5068
];
5169

5270
const locations = [
5371
// TODO add elements here
72+
"Amsterdam",
73+
"Breda",
74+
"Tilburg",
75+
"Utrecht",
76+
"Rotterdam",
5477
];
5578

5679
const jobTitles = [
5780
// TODO add elements here
81+
"Programmer",
82+
"Teacher",
83+
"Soldier",
84+
"Manager",
85+
"Musician",
5886
];
5987

6088
console.log(tellFortune(numKids, partnerNames, locations, jobTitles));
@@ -63,6 +91,6 @@ function main() {
6391
}
6492

6593
// ! Do not change or remove the code below
66-
if (process.env.NODE_ENV !== 'test') {
94+
if (process.env.NODE_ENV !== "test") {
6795
main();
6896
}

1-JavaScript/Week2/assignment/ex4-shoppingCart.js

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,48 +16,56 @@ you have more than 3 items in your shopping cart the first item gets taken out.
1616
1717
2. Confirm that your code passes the unit tests.
1818
-----------------------------------------------------------------------------*/
19-
const shoppingCart = ['bananas', 'milk'];
19+
const shoppingCart = ["bananas", "milk"];
2020

2121
// ! Function to be tested
22-
function addToShoppingCart(/* parameters go here */) {
22+
function addToShoppingCart(item /* parameters go here */) {
2323
// TODO complete this function
24+
if (!item) {
25+
return `You bought ${shoppingCart.join(", ")}!`;
26+
}
27+
// shoppingCart.length > 2 ? shoppingCart.splice(0, 1) : shoppingCart;
28+
shoppingCart.length > 2 && shoppingCart.splice(0, 1);
29+
30+
shoppingCart.push(item);
31+
return `You bought ${shoppingCart.join(", ")}!`;
2432
}
2533

2634
// ! Test functions (plain vanilla JavaScript)
2735
function test1() {
2836
console.log(
29-
'Test 1: addShoppingCart() called without an argument should leave the shopping cart unchanged'
37+
"Test 1: addShoppingCart() called without an argument should leave the shopping cart unchanged"
3038
);
31-
const expected = 'You bought bananas, milk!';
39+
const expected = "You bought bananas, milk!";
3240
const actual = addToShoppingCart();
3341
console.assert(actual === expected);
3442
}
3543

3644
function test2() {
37-
console.log('Test 2: addShoppingCart() should take one parameter');
45+
console.log("Test 2: addShoppingCart() should take one parameter");
3846
const expected = 1;
3947
const actual = addToShoppingCart.length;
4048
console.assert(actual === expected);
4149
}
4250

4351
function test3() {
44-
console.log('Test 3: `chocolate` should be added');
45-
const expected = 'You bought bananas, milk, chocolate!';
46-
const actual = addToShoppingCart('chocolate');
52+
console.log("Test 3: `chocolate` should be added");
53+
const expected = "You bought bananas, milk, chocolate!";
54+
const actual = addToShoppingCart("chocolate");
4755
console.assert(actual === expected);
4856
}
4957

5058
function test4() {
51-
console.log('Test 4: `waffles` should be added and `bananas` removed');
52-
const expected = 'You bought milk, chocolate, waffles!';
53-
const actual = addToShoppingCart('waffles');
59+
console.log("Test 4: `waffles` should be added and `bananas` removed");
60+
const expected = "You bought milk, chocolate, waffles!";
61+
const actual = addToShoppingCart("waffles");
5462
console.assert(actual === expected);
5563
}
5664

5765
function test5() {
58-
console.log('Test 5: `tea` should be added and `milk` removed');
59-
const expected = 'You bought chocolate, waffles, tea!';
60-
const actual = addToShoppingCart('tea');
66+
console.log("Test 5: `tea` should be added and `milk` removed");
67+
const expected = "You bought chocolate, waffles, tea!";
68+
const actual = addToShoppingCart("tea");
6169
console.assert(actual === expected);
6270
}
6371

1-JavaScript/Week2/assignment/ex5-shoppingCartPure.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,14 @@ it pure. Do the following:
1515
5. Confirm that you function passes the provided unit tests.
1616
------------------------------------------------------------------------------*/
1717
// ! Function under test
18-
function addToShoppingCart(/* TODO parameter(s) go here */) {
18+
function addToShoppingCart([...shoppingCart],item ) {
1919
// TODO complete this function
20+
if (!item) {
21+
return shoppingCart;
22+
}
23+
shoppingCart.length > 2 ? shoppingCart.splice(0, 1) : shoppingCart;
24+
shoppingCart.push(item);
25+
return shoppingCart;
2026
}
2127

2228
// ! Test functions (plain vanilla JavaScript)

1-JavaScript/Week2/assignment/ex6-totalCost.js

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,21 +21,34 @@ instead!
2121
-----------------------------------------------------------------------------*/
2222
const cartForParty = {
2323
// TODO complete this object
24+
chips: 1.75,
25+
juice: 2.4,
26+
frenchfries: 4.99,
27+
cake: 8.99,
28+
choclate: 2.99,
2429
};
2530

26-
function calculateTotalPrice(/* TODO parameter(s) go here */) {
31+
function calculateTotalPrice(obj/* TODO parameter(s) go here */) {
2732
// TODO replace this comment with your code
33+
let sum = 0;
34+
for (const [_, value] of Object.entries(obj)) {
35+
sum += value;
36+
}
37+
return Number(sum.toFixed(2));
2838
}
2939

3040
// ! Test functions (plain vanilla JavaScript)
3141
function test1() {
3242
console.log('\nTest 1: calculateTotalPrice should take one parameter');
33-
// TODO replace this comment with your code
43+
console.assert(calculateTotalPrice.length === 1);
44+
3445
}
3546

3647
function test2() {
3748
console.log('\nTest 2: return correct output when passed cartForParty');
38-
// TODO replace this comment with your code
49+
const expected = 21.12;
50+
const actual = calculateTotalPrice(cartForParty);
51+
console.assert(actual === expected);
3952
}
4053

4154
function test() {

1-JavaScript/Week2/assignment/ex7-mindPrivacy.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,13 @@ const employeeRecords = [
2929
];
3030

3131
// ! Function under test
32-
function filterPrivateData(/* TODO parameter(s) go here */) {
32+
function filterPrivateData(employeesRecord/* TODO parameter(s) go here */) {
3333
// TODO complete this function
34+
const nonPrivateData = [];
35+
for (let { name, occupation, email } of employeesRecord) {
36+
nonPrivateData.push({ name, occupation, email });
37+
}
38+
return nonPrivateData;
3439
}
3540

3641
// ! Test functions (plain vanilla JavaScript)

0 commit comments

Comments
 (0)