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 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
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
5 changes: 3 additions & 2 deletions 1-JavaScript/Week3/homework/ex2-dogYears.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ calculate it!
ages.
-----------------------------------------------------------------------------*/

function calculateDogAge(/* parameters go here */) {
// TODO complete this function
function calculateDogAge(age) {
const dogAge = age * 7;
return `Your doggie is ${dogAge} years old in dog years!`;
}

console.log(calculateDogAge(1)); // -> "Your doggie is 7 years old in dog years!"
Expand Down
32 changes: 18 additions & 14 deletions 1-JavaScript/Week3/homework/ex3-tellFortune.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,30 +25,34 @@ Note: The DRY is put into practice here: instead of repeating the code to
randomly select array elements four times inside the `tellFortune` function
body, this code is now written once only in a separated function.
-----------------------------------------------------------------------------*/
const numKids = [
// TODO add elements here
];
const numKids = [1, 2, 3, 4, 5];

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

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

function tellFortune(/* add parameter(s) here */) {
// TODO complete this function
function tellFortune(num, name, address, job) {
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
17 changes: 12 additions & 5 deletions 1-JavaScript/Week3/homework/ex4-shoppingCart.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,20 @@ 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 */) {
// TODO complete this function
function addToShoppingCart(groceryItem) {
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
9 changes: 7 additions & 2 deletions 1-JavaScript/Week3/homework/ex5-shoppingCartRedux.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,13 @@ 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 */) {
// TODO complete this function
function addToShoppingCart(arr, groceryItem) {
const newArr = [...arr];
newArr.push(groceryItem);
if (newArr.length > 3) {
newArr.shift();
}
return newArr;
}

const shoppingCart = ['bananas', 'milk'];
Expand Down
14 changes: 11 additions & 3 deletions 1-JavaScript/Week3/homework/ex6-totalCost.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,21 @@ 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}`;
}

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

console.log(calculateTotalPrice(cartForParty));

// this is one example, you will need to write a different object
calculateTotalPrice({
apples: 12,
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
2 changes: 2 additions & 0 deletions 1-JavaScript/Week3/test-reports/ex1-giveCompliment.fail.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*** Unit Test Error Report ***

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.

2 changes: 2 additions & 0 deletions 1-JavaScript/Week3/test-reports/ex3-tellFortune.fail.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*** Unit Test Error Report ***

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"
]
}
2 changes: 1 addition & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// https://jestjs.io/docs/en/configuration.html

module.exports = {
preset: 'jest-puppeteer',
// preset: 'jest-puppeteer',
maxConcurrency: 1,
extraGlobals: ['Math'],
};
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
}
}
}
Loading