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

Java script week3 #1

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 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
21 changes: 17 additions & 4 deletions 1-JavaScript/Week3/homework/ex1-giveCompliment.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,25 @@
Use `console.log` each time to display the return value of the
`giveCompliment` function to the console.
-----------------------------------------------------------------------------*/
function giveCompliment(/* TODO parameter(s) go here */) {
// TODO complete this function
function giveCompliment(name) {
const compliments = [
'great',
'awesome',
'lovely',
'amazing',
'wonderful',
'phenomenal',
'spectacular',
'nice',
'smart',
'strong',
];
const index = Math.floor(Math.random() * 10);
const compliment = compliments[index];
return `You are ${compliment}, ${name}!`;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very nicely done and good use of variables.

}

// TODO substitute your own name for "HackYourFuture"
const myName = 'HackYourFuture';
const myName = 'Veronika';

console.log(giveCompliment(myName));
console.log(giveCompliment(myName));
Expand Down
4 changes: 3 additions & 1 deletion 1-JavaScript/Week3/homework/ex2-dogYears.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ calculate it!
ages.
-----------------------------------------------------------------------------*/

function calculateDogAge(/* parameters go here */) {
function calculateDogAge(age) {
// TODO complete this function
age = age * 7;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can remove the TODO comments once you have implemented the logic. Also, in this case, I would recommend to create a new variable here(if needed) instead of mutating the function argument. It would be much cleaner. For example,

function calculateDogAge(age) {
  const dogAge = age * 7;
  return `Your doggie is ${dogAge} years old in dog years!`;
}

or

function calculateDogAge(age) {
  return `Your doggie is ${age * 7} years old in dog years!`;
}

return `Your doggie is ${age} years old in dog years!`;
}

console.log(calculateDogAge(1)); // -> "Your doggie is 7 years old in dog years!"
Expand Down
32 changes: 30 additions & 2 deletions 1-JavaScript/Week3/homework/ex3-tellFortune.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,28 +27,56 @@ body, this code is now written once only in a separated function.
-----------------------------------------------------------------------------*/
const numKids = [
// TODO add elements here
1,
2,
3,
4,
5,
];

const partnerNames = [
// TODO add elements here
'Thomas',
'James',
'Mia',
'Ruby',
'Scarlett',
];

const locations = [
// TODO add elements here
'Amsterdam',
'Utrecht',
'Rotterdam',
'Den Haag',
'Leiden',
];

const jobTitles = [
// TODO add elements here
'Director',
'Manager',
'Administrator',
'Web administrator',
'Zoologist',
];

// 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 */) {
function selectRandomly(arr) {
// TODO complete this function
const index = Math.floor(Math.random() * 5);
return arr[index];
}

function tellFortune(/* add parameter(s) here */) {
function tellFortune(num, name, address, job) {
// TODO complete this function
const jobTitle = selectRandomly(job);
const numKids = selectRandomly(num);
const location = selectRandomly(address);
const partnerName = selectRandomly(name);

return `You will be a ${jobTitle} in ${location}, married to ${partnerName} with ${numKids} kids.`;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

}

console.log(tellFortune(numKids, partnerNames, locations, jobTitles));
Expand Down
16 changes: 12 additions & 4 deletions 1-JavaScript/Week3/homework/ex4-shoppingCart.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,21 @@ you have more than 3 items in your shopping cart the first item gets taken out.
-----------------------------------------------------------------------------*/
const shoppingCart = ['bananas', 'milk'];

function addToShoppingCart(/* parameters go here */) {
function addToShoppingCart(groceryItem) {
// TODO complete this function
if (shoppingCart.length < 3) {
shoppingCart.push(groceryItem);
} else {
shoppingCart.shift();
shoppingCart.push(groceryItem);
}
const groceriesString = shoppingCart.join(', ');
return `You bought ${groceriesString}!`;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

}

addToShoppingCart('chocolate'); // Returns "You bought bananas, milk, chocolate!"
addToShoppingCart('waffles'); // Returns "You bought milk, chocolate, waffles!"
addToShoppingCart('tea'); // Returns "You bought chocolate, waffles, tea!"
console.log(addToShoppingCart('chocolate')); // Returns "You bought bananas, milk, chocolate!"
console.log(addToShoppingCart('waffles')); // Returns "You bought milk, chocolate, waffles!"
console.log(addToShoppingCart('tea')); // Returns "You bought chocolate, waffles, tea!"

// ! Do not change or remove any code below
module.exports = {
Expand Down
10 changes: 9 additions & 1 deletion 1-JavaScript/Week3/homework/ex5-shoppingCartRedux.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,16 @@ it pure. Do the following:
4. When constructing the new shopping cart array you should make use of the ES5
spread syntax.
------------------------------------------------------------------------------*/
function addToShoppingCart(/* TODO parameter(s) go here */) {
function addToShoppingCart(arr, groceryItem) {
// TODO complete this function
const newArr = [...arr];
if (newArr.length < 3) {
newArr.push(groceryItem);
} else {
newArr.shift();
newArr.push(groceryItem);
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if you did this instead? A bit cleaner, right?

  const newArr = [...arr];
  newArr.push(groceryItem);

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

return newArr;
}

const shoppingCart = ['bananas', 'milk'];
Expand Down
16 changes: 13 additions & 3 deletions 1-JavaScript/Week3/homework/ex6-totalCost.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,22 @@ instead!
Use `console.log` to display the result.
-----------------------------------------------------------------------------*/
const cartForParty = {
// TODO complete this function
beers: 1.1,
chips: 2.9,
cola: 2.3,
olives: 2.7,
bacon: 2, //11.00
};

function calculateTotalPrice(/* TODO parameter(s) go here */) {
// TODO replace this comment with your code
function calculateTotalPrice(cart) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi Veronica - although this logic works fine but you could improve it slightly. Firstly, you should try to avoid using for-in loops for iterating over objects. Try to use Object.keys() instead. The difference between the two can be found out on google, it's something to do regarding iterating over parent object properties. Secondly, if you use Object.keys, you could also look at using the array's reduce method. Something like below:

function calculateTotalPrice(cart) {
  const total = Object.keys(cart).reduce((acc, elem) => acc + cart[elem], 0);
  return `Total: ${total}`;
}

let totalPrice = 0;
for (const key in cart) {
totalPrice = totalPrice + cart[key];
}
totalPrice = totalPrice.toFixed(2);
return `Total: €${totalPrice}`;
}
console.log(calculateTotalPrice(cartForParty));

// this is one example, you will need to write a different object
calculateTotalPrice({
Expand Down
9 changes: 8 additions & 1 deletion 1-JavaScript/Week3/homework/ex7-mindPrivacy.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,15 @@ const employeeRecords = [
},
];

function filterPrivateData(/* parameter(s) go here */) {
function filterPrivateData(arrEmployees) {
// TODO complete this function
const newArrEmployees = [];
for (const employee of arrEmployees) {
const { name, occupation, email } = employee;
const employeeNew = { name, occupation, email };
newArrEmployees.push(employeeNew);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🆗

}
return newArrEmployees;
}

console.log(filterPrivateData(employeeRecords));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
All tests passed

This file was deleted.

1 change: 1 addition & 0 deletions 1-JavaScript/Week3/test-reports/ex2-dogYears.pass.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
All tests passed
1 change: 0 additions & 1 deletion 1-JavaScript/Week3/test-reports/ex2-dogYears.todo.txt

This file was deleted.

1 change: 1 addition & 0 deletions 1-JavaScript/Week3/test-reports/ex3-tellFortune.pass.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
All tests passed
1 change: 0 additions & 1 deletion 1-JavaScript/Week3/test-reports/ex3-tellFortune.todo.txt

This file was deleted.

1 change: 1 addition & 0 deletions 1-JavaScript/Week3/test-reports/ex4-shoppingCart.pass.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
All tests passed
1 change: 0 additions & 1 deletion 1-JavaScript/Week3/test-reports/ex4-shoppingCart.todo.txt

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
All tests passed

This file was deleted.

1 change: 1 addition & 0 deletions 1-JavaScript/Week3/test-reports/ex6-totalCost.pass.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
All tests passed
1 change: 0 additions & 1 deletion 1-JavaScript/Week3/test-reports/ex6-totalCost.todo.txt

This file was deleted.

1 change: 1 addition & 0 deletions 1-JavaScript/Week3/test-reports/ex7-mindPrivacy.pass.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
All tests passed
1 change: 0 additions & 1 deletion 1-JavaScript/Week3/test-reports/ex7-mindPrivacy.todo.txt

This file was deleted.

3 changes: 3 additions & 0 deletions cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,21 @@
"clonedeep",
"Cryptr",
"eqeqeq",
"Haag",
"hplogo",
"networkidle",
"Philipp",
"pokemons",
"printf",
"rethrowing",
"Scarlett",
"sysinfo",
"systeminformation",
"testx",
"thruthy",
"typeof",
"usingapis",
"Veronika",
"Woww"
]
}
58 changes: 58 additions & 0 deletions sysinfo.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
{
"node": "v14.15.4",
"system": {
"manufacturer": "Acer",
"model": "Aspire ES1-331"
},
"cpu": {
"manufacturer": "Intel®",
"brand": "Pentium® N3700",
"speed": 1.6,
"virtualization": true
},
"memory": "8 GB",
"osInfo": {
"platform": "win32",
"distro": "Microsoft Windows 10 Home Single Language",
"release": "10.0.18363"
},
"blockDevices": [
{
"name": "C:",
"size": "499 GB",
"fsType": "ntfs",
"physical": "Local"
}
],
"vscodeInfo": {
"version": "1.53.0",
"extensions": [
"CoenraadS.bracket-pair-colorizer",
"dbaeumer.vscode-eslint",
"esbenp.prettier-vscode",
"evgeniypeshkov.syntax-highlighter",
"formulahendry.code-runner",
"hdg.live-html-previewer",
"ritwickdey.LiveServer",
"streetsidesoftware.code-spell-checker",
"techer.open-in-browser"
],
"userSettings": {
"editor.detectIndentation": false,
"editor.formatOnSave": true,
"editor.minimap.enabled": false,
"editor.renderIndentGuides": true,
"editor.tabSize": 2,
"editor.codeActionsOnSave": {
"source.fixAll": true
},
"eslint.autoFixOnSave": true,
"files.autoSave": "onFocusChange",
"prettier.singleQuote": true,
"prettier.trailingComma": "all",
"terminal.integrated.shell.windows": "C:\\Program Files\\Git\\bin\\bash.exe",
"workbench.colorTheme": "Visual Studio Light",
"window.zoomLevel": 1
}
}
}
81 changes: 81 additions & 0 deletions [email protected]
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
2021-02-10 06:20:02 info: ----------------------------------------------
2021-02-10 06:20:02 info: >>> Running Unit Test `ex1-giveCompliment` <<<
2021-02-10 06:20:02 info: ----------------------------------------------
2021-02-10 06:20:50 error: *** Unit Test Error Report ***



2021-02-10 06:22:38 info: Disclaimer turned off
2021-02-10 06:27:32 info: ----------------------------------------------
2021-02-10 06:27:32 info: >>> Running Unit Test `ex1-giveCompliment` <<<
2021-02-10 06:27:32 info: ----------------------------------------------
2021-02-10 06:27:54 info: All unit tests passed.
2021-02-10 06:28:05 info: All steps were completed successfully
2021-02-10 07:21:59 info: ----------------------------------------
2021-02-10 07:21:59 info: >>> Running Unit Test `ex2-dogYears` <<<
2021-02-10 07:21:59 info: ----------------------------------------
2021-02-10 07:22:17 info: All unit tests passed.
2021-02-10 07:22:31 info: All steps were completed successfully
2021-02-11 10:24:25 info: -------------------------------------------
2021-02-11 10:24:25 info: >>> Running Unit Test `ex3-tellFortune` <<<
2021-02-11 10:24:25 info: -------------------------------------------
2021-02-11 10:25:14 info: All unit tests passed.
2021-02-11 10:25:37 error: *** Spell Checker Report ***

ex3-tellFortune.js:43:4 - Unknown word (Scarlett)
ex3-tellFortune.js:51:8 - Unknown word (Haag)

2021-02-11 11:07:31 info: -------------------------------------------
2021-02-11 11:07:31 info: >>> Running Unit Test `ex3-tellFortune` <<<
2021-02-11 11:07:31 info: -------------------------------------------
2021-02-11 11:08:29 error: *** Unit Test Error Report ***



2021-02-11 11:09:26 info: -------------------------------------------
2021-02-11 11:09:26 info: >>> Running Unit Test `ex3-tellFortune` <<<
2021-02-11 11:09:26 info: -------------------------------------------
2021-02-11 11:09:53 info: All unit tests passed.
2021-02-11 11:10:05 info: All steps were completed successfully
2021-02-11 11:44:24 info: --------------------------------------------
2021-02-11 11:44:24 info: >>> Running Unit Test `ex4-shoppingCart` <<<
2021-02-11 11:44:24 info: --------------------------------------------
2021-02-11 11:44:44 info: All unit tests passed.
2021-02-11 11:44:57 info: All steps were completed successfully
2021-02-12 12:06:22 info: -------------------------------------------------
2021-02-12 12:06:22 info: >>> Running Unit Test `ex5-shoppingCartRedux` <<<
2021-02-12 12:06:22 info: -------------------------------------------------
2021-02-12 12:06:43 info: All unit tests passed.
2021-02-12 12:06:55 info: All steps were completed successfully
2021-02-12 12:29:45 info: -----------------------------------------
2021-02-12 12:29:45 info: >>> Running Unit Test `ex6-totalCost` <<<
2021-02-12 12:29:45 info: -----------------------------------------
2021-02-12 12:30:04 error: *** Unit Test Error Report ***

- calculateTotalPrice cartForParty should contain five grocery items with prices
- calculateTotalPrice should return the total price in Euros
Expected: Total: €12.00
Received: Total: €12

2021-02-12 12:33:02 info: -----------------------------------------
2021-02-12 12:33:02 info: >>> Running Unit Test `ex6-totalCost` <<<
2021-02-12 12:33:02 info: -----------------------------------------
2021-02-12 12:33:19 info: All unit tests passed.
2021-02-12 12:33:31 info: All steps were completed successfully
2021-02-12 11:29:19 info: -------------------------------------------
2021-02-12 11:29:19 info: >>> Running Unit Test `ex7-mindPrivacy` <<<
2021-02-12 11:29:19 info: -------------------------------------------
2021-02-12 11:29:37 info: All unit tests passed.
2021-02-12 11:29:49 info: All steps were completed successfully
2021-02-15 12:27:15 info: -----------------------------------------
2021-02-15 12:27:15 info: >>> Running Unit Test `ex6-totalCost` <<<
2021-02-15 12:27:15 info: -----------------------------------------
2021-02-15 12:27:55 error: *** Unit Test Error Report ***



2021-02-15 12:28:33 info: -----------------------------------------
2021-02-15 12:28:33 info: >>> Running Unit Test `ex6-totalCost` <<<
2021-02-15 12:28:33 info: -----------------------------------------
2021-02-15 12:28:57 info: All unit tests passed.
2021-02-15 12:29:09 info: All steps were completed successfully