-
Notifications
You must be signed in to change notification settings - Fork 0
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
base: main
Are you sure you want to change the base?
Changes from all commits
9d8a002
9dc6857
56b0c0a
c19428b
bc795fd
ecefef1
be7330b
1257598
b95b748
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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.`; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
} | ||
|
||
console.log(tellFortune(numKids, partnerNames, locations, jobTitles)); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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}!`; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 = { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
|
||
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, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🆗 |
||
} | ||
return newArrEmployees; | ||
} | ||
|
||
console.log(filterPrivateData(employeeRecords)); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
*** Unit Test Error Report *** | ||
|
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
All tests passed |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
*** Unit Test Error Report *** | ||
|
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
All tests passed |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
All tests passed |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
All tests passed |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
All tests passed |
This file was deleted.
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 | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
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.