Skip to content

JAN-25 | MIKIYAS GEBREMICHAEL | DATA FLOWS | Destructuring | SPRINT-1 | WEEK - 1 #161

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

Closed
Closed
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
4 changes: 3 additions & 1 deletion Sprint-1/destructuring/exercise-1/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ const personOne = {
age: 34,
favouriteFood: "Spinach",
};
let {name, age, favouriteFood} = personOne;
console.log(`${name} and ${age}`);

// Update the parameter to this function to make it work.
// Don't change anything else.
function introduceYourself(___________________________) {
function introduceYourself({name,age,favouriteFood}) {
console.log(
`Hello, my name is ${name}. I am ${age} years old and my favourite food is ${favouriteFood}.`
);
Expand Down
26 changes: 26 additions & 0 deletions Sprint-1/destructuring/exercise-2/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,29 @@ let hogwarts = [
occupation: "Teacher",
},
];
//Task 1
function searchByHouse(arr, houseName) {
let result = [];
for (let n of arr) {
let { firstName, lastName, house } = n;
if (house === houseName) {
result.push(`${firstName} ${lastName}`);
}
}
return result.join("\n");
}
//Task 2
function havePet(arr) {
let result = [];
for (let n of arr) {
let { firstName, lastName, pet, occupation } = n;
if (occupation === "Teacher" && pet !== null && pet !== undefined && pet !== "") {
result.push(`${firstName} ${lastName}`);
}
}
return result.join("\n");
}

// Test the functions
console.log(searchByHouse(hogwarts, "Gryffindor"));
console.log(havePet(hogwarts));
30 changes: 30 additions & 0 deletions Sprint-1/destructuring/exercise-3/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,33 @@ let order = [
{ itemName: "Hot Coffee", quantity: 2, unitPricePence: 100 },
{ itemName: "Hash Brown", quantity: 4, unitPricePence: 40 },
];

//this function calculate the total of our array of objects and return the total in pounds.
function findTotal(arrayInput) {
let priceItem = [];
for (let n of arrayInput) {
let { itemName, quantity, unitPricePence } = n;
priceItem.push(unitPricePence * quantity); //i pushed all individual item prices to my array.
}
return priceItem.reduce((a, b) => a + b, 0) / 100; //suming using reduce method , and change the value to pounds
}
// Print the receipt with itemized list and total
function receiptPrinter(arrayInput) {
let headerLine = "QTY ITEM TOTAL";
let resultArr = [];
let total = findTotal(arrayInput); //i get the total using higher order function findTotal.
for (let n of arrayInput) {
let { itemName, quantity, unitPricePence } = n;
let totalItemPrice = (unitPricePence * quantity) / 100;
let quantityCol = String(quantity).padEnd(5); //this makes sure that the quantity column is 5 characters long.
let itemCol = itemName.padEnd(20);
let priceCol = totalItemPrice.toFixed(2).padStart(5);
resultArr.push(`${quantityCol}${itemCol}${priceCol}`); // storing each item's data in an array. no need to make space between the columns since the padEnd and padStart methods are used.
}
//the content of the reciept to be seen in the console
console.log(headerLine);
console.log(resultArr.join("\n"));
console.log(`Total: ${total.toFixed(2)}`);
}

receiptPrinter(order);