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..f838e89b 100644 --- a/array-destructuring/exercise-1/exercise.js +++ b/array-destructuring/exercise-1/exercise.js @@ -1,13 +1,14 @@ -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); 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..7e616f40 100644 --- a/array-destructuring/exercise-3/exercise.js +++ b/array-destructuring/exercise-3/exercise.js @@ -6,3 +6,26 @@ 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)); + 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(); }); } -} +}; + +