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

CYF London | Rihanna Poursoltani | Module-Data-Flows | Sprint 1 | Feature/destructuring #140

Open
wants to merge 3 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
4 changes: 2 additions & 2 deletions Sprint-1/destructuring/exercise-1/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ const personOne = {

// 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}.`
`Hello, my name is ${name}. I am ${age} years old and my favourit food is ${favouriteFood}.`
);
}

Expand Down
3 changes: 3 additions & 0 deletions Sprint-1/destructuring/exercise-1/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,7 @@ console.log(`Batman is ${firstName}, ${lastName}`);
# Exercise

- What is the syntax to destructure the object `personOne` in exercise.js?
const { name, age, favouriteFood } = personOne;
The {} braces are used for destructuring and they exctract object values

- Update the parameter of the function `introduceYourself` to use destructuring on the object that gets passed in.
18 changes: 18 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,21 @@ let hogwarts = [
occupation: "Teacher",
},
];
function displayGryffindorMembers(hogwarts){
hogwarts.forEach(({firstName, lastName, house}) => {
if (house == "Gryffindor"){
console.log(`${firstName} ${lastName}`);
}
});
}
displayGryffindorMembers(hogwarts);

function displayTeacherWpet(hogwarts){
hogwarts.forEach(({firstName, lastName, occupation, pet}) => {
if ((occupation == "Teacher") && (pet !== null)){
console.log(`${firstName} ${lastName}`);
}
});
}

displayTeacherWpet(hogwarts);
16 changes: 15 additions & 1 deletion Sprint-1/destructuring/exercise-3/exercise.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,22 @@
let order = [
{ itemName: "Hot cakes", quantity: 1, unitPricePence: 232 },
{ itemName: "Hot cakes", quantity: 12, unitPricePence: 232 },
{ itemName: "Apple Pie", quantity: 2, unitPricePence: 139 },
{ itemName: "Egg McMuffin", quantity: 1, unitPricePence: 280 },
{ itemName: "Sausage McMuffin", quantity: 1, unitPricePence: 300 },
{ itemName: "Hot Coffee", quantity: 2, unitPricePence: 100 },
{ itemName: "Hash Brown", quantity: 4, unitPricePence: 40 },
];

function createReceipt(order){
let total = 0;
console.log("QTY".padEnd(7) + "ITEM".padEnd(30) + "TOTAL".padStart(10));

order.forEach(element => {
let {quantity, itemName, unitPricePence} = element;
let totalPriceItem = (quantity * unitPricePence) /100;
total += totalPriceItem;
console.log(String(quantity).padEnd(7)+ itemName.padEnd(30) + totalPriceItem.toFixed(2).padStart(10))
});
console.log(`Total: ${total.toFixed(2)}`);
}
createReceipt(order);
25 changes: 15 additions & 10 deletions debugging/book-library/script.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
let myLibrary = [];

window.addEventListener("load", function (e) {
window.addEventListener("load", function () {
populateStorage();
render();
});
Expand Down Expand Up @@ -37,8 +37,10 @@ function submit() {
alert("Please fill all fields!");
return false;
} else {
let book = new Book(title.value, title.value, pages.value, check.checked);
library.push(book);
//Correctly pass author.value
let book = new Book(title.value, author.value, pages.value, check.checked);
//Replace library.push(book) with myLibrary.push(book)
myLibrary.push(book);
render();
}
}
Expand All @@ -54,7 +56,7 @@ function render() {
let table = document.getElementById("display");
let rowsNumber = table.rows.length;
//delete old table
for (let n = rowsNumber - 1; n > 0; n-- {
for (let n = rowsNumber - 1; n > 0; n--) {
table.deleteRow(n);
}
//insert updated row and cells
Expand All @@ -76,7 +78,8 @@ function render() {
changeBut.className = "btn btn-success";
wasReadCell.appendChild(changeBut);
let readStatus = "";
if (myLibrary[i].check == false) {
//Fix the logic for the "Read" statues true for "Yes", false for No
if (myLibrary[i].check == true) {
readStatus = "Yes";
} else {
readStatus = "No";
Expand All @@ -89,12 +92,14 @@ function render() {
});

//add delete button to every row and render again
// correct delButton in rows
let delButton = document.createElement("button");
delBut.id = i + 5;
deleteCell.appendChild(delBut);
delBut.className = "btn btn-warning";
delBut.innerHTML = "Delete";
delBut.addEventListener("clicks", function () {
delButton.id = i + 5;
deleteCell.appendChild(delButton);
delButton.className = "btn btn-warning";
delButton.innerHTML = "Delete";
//Fix the event listener: "click" instead of "clicks".
delButton.addEventListener("click", function () {
alert(`You've deleted title: ${myLibrary[i].title}`);
myLibrary.splice(i, 1);
render();
Expand Down