From 2c02a40f9cc213e19a06085889d8981d97804929 Mon Sep 17 00:00:00 2001 From: Bakhat Begum Date: Mon, 12 Feb 2024 18:52:02 +0000 Subject: [PATCH 1/6] I added a line that was accidentally removed --- book-library/script.js | 62 ++++++++++++++++++++++-------------------- 1 file changed, 32 insertions(+), 30 deletions(-) diff --git a/book-library/script.js b/book-library/script.js index dc14a775..1eac06f7 100644 --- a/book-library/script.js +++ b/book-library/script.js @@ -28,19 +28,24 @@ const check = document.getElementById("check"); //check the right input from forms and if its ok -> add the new book (object in array) //via Book function and start render function function submit() { + let titleValue = title.value; + let authorValue = author.value; + let pagesValue = pages.value; + let checkValue = check.checked; if ( - title.value == null || - title.value == "" || - pages.value == null || - pages.value == "" + titleValue == null || + authorValue == "" || + pagesValue == null || + checkValue == "" ) { alert("Please fill all fields!"); - return false; - } else { - let book = new Book(title.value, title.value, pages.value, check.checked); - library.push(book); - render(); } + + else { + let book = new Book(titleValue, authorValue, pagesValue, checkValue); + myLibrary.push(book); + render(); +} } function Book(title, author, pages, check) { @@ -49,39 +54,34 @@ function Book(title, author, pages, check) { this.pages = pages; this.check = check; } - 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 let length = myLibrary.length; for (let i = 0; i < length; i++) { - let row = table.insertRow(1); - let cell1 = row.insertCell(0); - let cell2 = row.insertCell(1); - let cell3 = row.insertCell(2); - let cell4 = row.insertCell(3); - let cell5 = row.insertCell(4); + const row = table.insertRow(1); + const cell1 = row.insertCell(0); + const cell2 = row.insertCell(1); + const cell3 = row.insertCell(2); + const cell4 = row.insertCell(3); + const cell5 = row.insertCell(4); cell1.innerHTML = myLibrary[i].title; cell2.innerHTML = myLibrary[i].author; cell3.innerHTML = myLibrary[i].pages; //add and wait for action for read/unread button - let changeBut = document.createElement("button"); + const changeBut = document.createElement("button"); changeBut.id = i; changeBut.className = "btn btn-success"; cell4.appendChild(changeBut); - let readStatus = ""; - if (myLibrary[i].check == false) { - readStatus = "Yes"; - } else { - readStatus = "No"; - } - changeBut.innerHTML = readStatus; + + const readStatus = myLibrary[i].check ? "Yes" : "No"; + changeBut.textContent = readStatus; changeBut.addEventListener("click", function () { myLibrary[i].check = !myLibrary[i].check; @@ -89,15 +89,17 @@ function render() { }); //add delete button to every row and render again - let delButton = document.createElement("button"); - delBut.id = i + 5; + const delBut = document.createElement("button"); + delBut.id = i; cell5.appendChild(delBut); delBut.className = "btn btn-warning"; - delBut.innerHTML = "Delete"; - delBut.addEventListener("clicks", function () { + delBut.innerText = "Delete"; + delBut.addEventListener("click", function () { alert(`You've deleted title: ${myLibrary[i].title}`); myLibrary.splice(i, 1); render(); }); } -} +}; + + From c42bd756ceb5b03a867116e7fae68c0de0f7278d Mon Sep 17 00:00:00 2001 From: Bakhat Begum Date: Wed, 14 Feb 2024 14:14:25 +0000 Subject: [PATCH 2/6] I destructure an objest and also update the functions argumnet. --- .vscode/launch.json | 17 +++++++++++++++++ array-destructuring/exercise-1/exercise.js | 13 ++++++++----- array-destructuring/exercise-2/exercise.js | 19 +++++++++++++++++++ array-destructuring/exercise-3/exercise.js | 20 ++++++++++++++++++++ 4 files changed, 64 insertions(+), 5 deletions(-) create mode 100644 .vscode/launch.json diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 00000000..4e7c6cad --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,17 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "type": "node", + "request": "launch", + "name": "Launch Program", + "skipFiles": [ + "/**" + ], + "program": "${workspaceFolder}/array-destructuring/exercise-2/exercise.js" + } + ] +} \ No newline at end of file diff --git a/array-destructuring/exercise-1/exercise.js b/array-destructuring/exercise-1/exercise.js index a6eab299..fe2c7b2b 100644 --- a/array-destructuring/exercise-1/exercise.js +++ b/array-destructuring/exercise-1/exercise.js @@ -1,13 +1,16 @@ -const personOne = { +let personOne = { name: "Popeye", age: 34, - favouriteFood: "Spinach", + favoriteFood: "Spinach", }; -function introduceYourself(___________________________) { +let { name, age, favoriteFood } = personOne; + +function introduceYourself({ name, age, favoriteFood }) { 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 favorite food is ${favoriteFood}.` ); } -introduceYourself(personOne); +console.log(introduceYourself(personOne)); + diff --git a/array-destructuring/exercise-2/exercise.js b/array-destructuring/exercise-2/exercise.js index e11b75eb..31c3cfd6 100644 --- a/array-destructuring/exercise-2/exercise.js +++ b/array-destructuring/exercise-2/exercise.js @@ -70,3 +70,22 @@ let hogwarts = [ occupation: "Teacher", }, ]; + +let { firstName, lastName, house, pet} = hogwarts; + +function property(array){ + let gryfindorHouse = array.filter(hogwarts => hogwarts.house === "Gryffindor") //filter mean to give only the elements that pass a certain condition specified by a provided function + let getAllName = gryfindorHouse.map( hogwarts =>`${hogwarts.firstName}${hogwarts.lastName}`); + return getAllName; +} +console.log(property(hogwarts)); + +function whoHavePet(array){ + let findPet = array.filter(hogwarts => hogwarts.pet !== null) + let getPet = findPet.map( hogwarts =>`${hogwarts.firstName}${hogwarts.lastName}`); + return getPet; +} + +console.log(whoHavePet(hogwarts)); + + diff --git a/array-destructuring/exercise-3/exercise.js b/array-destructuring/exercise-3/exercise.js index 0a01f8f0..54fb742b 100644 --- a/array-destructuring/exercise-3/exercise.js +++ b/array-destructuring/exercise-3/exercise.js @@ -6,3 +6,23 @@ let order = [ { itemName: "Hot Coffee", quantity: 2, unitPrice: 1.0 }, { itemName: "Hash Brown", quantity: 4, unitPrice: 0.4 }, ]; + +let { quantity, itemName, unitPrice } = order; + +function itemList(item){ + const subtitleItems = ["QTY", "ITEM", "TOTAL"]; + item.forEach(order => { + const totalOrder = (order.quantity * order.unitPrice).toFixed(2); + subtitleItems.push(`${order.quantity}\t${order.itemName}\t${totalOrder}`); + }); + return subtitleItems.join('\n') +} +console.log(itemList(order)); + + +function totalPrice(num) { + const total = num.reduce((accumulator, currentValue) => accumulator + currentValue.unitPrice * currentValue.quantity, 0); + return `total: ${total}` +} + console.log(totalPrice(order)); + From b023e0678dc2ffa51ecd430e049b4bcb7c69d4d1 Mon Sep 17 00:00:00 2001 From: Bakhat Begum Date: Wed, 14 Feb 2024 14:16:45 +0000 Subject: [PATCH 3/6] added line space. --- array-destructuring/exercise-3/exercise.js | 1 + 1 file changed, 1 insertion(+) diff --git a/array-destructuring/exercise-3/exercise.js b/array-destructuring/exercise-3/exercise.js index 54fb742b..0db4ced2 100644 --- a/array-destructuring/exercise-3/exercise.js +++ b/array-destructuring/exercise-3/exercise.js @@ -23,6 +23,7 @@ console.log(itemList(order)); function totalPrice(num) { const total = num.reduce((accumulator, currentValue) => accumulator + currentValue.unitPrice * currentValue.quantity, 0); return `total: ${total}` + } console.log(totalPrice(order)); From a48af942520b54b9be39ca4e3c30c93e89c9136d Mon Sep 17 00:00:00 2001 From: Bakhat Begum Date: Wed, 14 Feb 2024 14:19:38 +0000 Subject: [PATCH 4/6] added line space. --- array-destructuring/exercise-3/exercise.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/array-destructuring/exercise-3/exercise.js b/array-destructuring/exercise-3/exercise.js index 0db4ced2..9b13a433 100644 --- a/array-destructuring/exercise-3/exercise.js +++ b/array-destructuring/exercise-3/exercise.js @@ -15,6 +15,7 @@ function itemList(item){ const totalOrder = (order.quantity * order.unitPrice).toFixed(2); subtitleItems.push(`${order.quantity}\t${order.itemName}\t${totalOrder}`); }); + return subtitleItems.join('\n') } console.log(itemList(order)); @@ -23,7 +24,7 @@ console.log(itemList(order)); function totalPrice(num) { const total = num.reduce((accumulator, currentValue) => accumulator + currentValue.unitPrice * currentValue.quantity, 0); return `total: ${total}` - + } console.log(totalPrice(order)); From 1e475f82e48586a926f4897f2b1bf4166973c3ae Mon Sep 17 00:00:00 2001 From: Bakhat Begum Date: Wed, 14 Feb 2024 14:36:35 +0000 Subject: [PATCH 5/6] added line space and removed commented lines --- array-destructuring/exercise-3/exercise.js | 1 + 1 file changed, 1 insertion(+) diff --git a/array-destructuring/exercise-3/exercise.js b/array-destructuring/exercise-3/exercise.js index 9b13a433..7e616f40 100644 --- a/array-destructuring/exercise-3/exercise.js +++ b/array-destructuring/exercise-3/exercise.js @@ -17,6 +17,7 @@ function itemList(item){ }); return subtitleItems.join('\n') + } console.log(itemList(order)); From 3efbd1fb2b9d180802bde3ca6ee6f74c9766d792 Mon Sep 17 00:00:00 2001 From: Bakhat Begum Date: Wed, 14 Feb 2024 22:47:34 +0000 Subject: [PATCH 6/6] i removed function variable --- array-destructuring/exercise-1/exercise.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/array-destructuring/exercise-1/exercise.js b/array-destructuring/exercise-1/exercise.js index fe2c7b2b..f838e89b 100644 --- a/array-destructuring/exercise-1/exercise.js +++ b/array-destructuring/exercise-1/exercise.js @@ -12,5 +12,3 @@ function introduceYourself({ name, age, favoriteFood }) { ); } -console.log(introduceYourself(personOne)); -