diff --git a/Sprint-1/destructuring/exercise-1/exercise.js b/Sprint-1/destructuring/exercise-1/exercise.js index 1ff2ac5c..90eb4795 100644 --- a/Sprint-1/destructuring/exercise-1/exercise.js +++ b/Sprint-1/destructuring/exercise-1/exercise.js @@ -6,7 +6,7 @@ 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}.` ); diff --git a/Sprint-1/destructuring/exercise-2/exercise.js b/Sprint-1/destructuring/exercise-2/exercise.js index e11b75eb..3189b381 100644 --- a/Sprint-1/destructuring/exercise-2/exercise.js +++ b/Sprint-1/destructuring/exercise-2/exercise.js @@ -70,3 +70,27 @@ let hogwarts = [ occupation: "Teacher", }, ]; + +function nameAndHoues(arr) { + let namesOfGryffindor = ""; + for (pepole of hogwarts) { + const { firstName, house, lastName } = pepole; + if (house === "Gryffindor") { + namesOfGryffindor += `${firstName} ${lastName}\n`; + } + } + return namesOfGryffindor; +} + +function staffPets(arr) { + let namesOfTeachersPets = ""; + for (let i = 0; i < arr.length; i++) { + const { firstName, pet, occupation, lastName } = arr[i]; + if (occupation == "Teacher" && pet !== null) + namesOfTeachersPets += `${firstName} ${lastName}`; + } + return namesOfTeachersPets; +} + +console.log(nameAndHoues(hogwarts)); +console.log(staffPets(hogwarts)); diff --git a/Sprint-1/destructuring/exercise-3/exercise.js b/Sprint-1/destructuring/exercise-3/exercise.js index b3a36f4e..95123a5b 100644 --- a/Sprint-1/destructuring/exercise-3/exercise.js +++ b/Sprint-1/destructuring/exercise-3/exercise.js @@ -6,3 +6,25 @@ let order = [ { itemName: "Hot Coffee", quantity: 2, unitPricePence: 100 }, { itemName: "Hash Brown", quantity: 4, unitPricePence: 40 }, ]; + +function receipt(arr) { + let totalPrice = 0; + let subTotal = 0; + let totals = ""; + for (let i = 0; i < order.length; i++) { + const { itemName, quantity, unitPricePence } = arr[i]; + + subTotal = unitPricePence * quantity; + let unitPricePenceAsCash = unitPricePence / 100; + unitPricePenceAsCash = unitPricePenceAsCash.toFixed(2); + totalPrice += subTotal; + + totals = quantity + itemName + unitPricePenceAsCash; + // totals = `${quantity} ${itemName} ${unitPricePenceAsCash}` + + console.log("%c", totals); + } + + console.log(` total ${totalPrice}`); +} +console.log(receipt(order)); diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index 75ce6c1d..36c4945a 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -5,41 +5,49 @@ window.addEventListener("load", function (e) { render(); }); + + + function populateStorage() { - if (myLibrary.length == 0) { + if (myLibrary.length === 0) { let book1 = new Book("Robison Crusoe", "Daniel Defoe", "252", true); let book2 = new Book( "The Old Man and the Sea", - "Ernest Hemingway", - "127", - true - ); - myLibrary.push(book1); - myLibrary.push(book2); - render(); + "Ernest Hemingway", "5", true); + myLibrary.push(book1, book2); + + + render(); } } -const title = document.getElementById("title"); -const author = document.getElementById("author"); -const pages = document.getElementById("pages"); -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() { + const title = document.getElementById("title").value; + const author = document.getElementById("author").value; + const pages = document.getElementById("pages").value; + const check = document.getElementById("check").checked; if ( - title.value == null || - title.value == "" || - pages.value == null || - pages.value == "" - ) { + + title == "" || + pages == ""|| + author == "" + ) + + { alert("Please fill all fields!"); - return false; + return true; } else { - let book = new Book(title.value, title.value, pages.value, check.checked); - library.push(book); + let book = new Book(title, author, pages, check); + myLibrary.push(book); render(); + //clear the input values + + } } @@ -54,12 +62,17 @@ 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 length = myLibrary.length ; + for (let i = 0; i <= length; i++) { + let row = table.insertRow(1); let titleCell = row.insertCell(0); let authorCell = row.insertCell(1); @@ -69,6 +82,11 @@ function render() { titleCell.innerHTML = myLibrary[i].title; authorCell.innerHTML = myLibrary[i].author; pagesCell.innerHTML = myLibrary[i].pages; + + //deleteCell.innerHTML = delBut[i] + + + //add and wait for action for read/unread button let changeBut = document.createElement("button"); @@ -76,7 +94,7 @@ function render() { changeBut.className = "btn btn-success"; wasReadCell.appendChild(changeBut); let readStatus = ""; - if (myLibrary[i].check == false) { + if (myLibrary[i].check == true) { readStatus = "Yes"; } else { readStatus = "No"; @@ -84,20 +102,25 @@ function render() { changeBut.innerText = readStatus; changeBut.addEventListener("click", function () { - myLibrary[i].check = !myLibrary[i].check; - render(); - }); + if( myLibrary[i].check == true){ + myLibrary[i].check = false; + } + else { + myLibrary[i].check = true; + } + + render(); + }); //add delete button to every row and render again - let delButton = document.createElement("button"); - delBut.id = i + 5; + let delBut = document.createElement("button"); + delBut.id = i ; deleteCell.appendChild(delBut); delBut.className = "btn btn-warning"; delBut.innerHTML = "Delete"; - delBut.addEventListener("clicks", function () { + delBut.addEventListener("click", function () { alert(`You've deleted title: ${myLibrary[i].title}`); myLibrary.splice(i, 1); render(); - }); + })}; } -} diff --git a/debugging/code-reading/readme.md b/debugging/code-reading/readme.md index 4090c14c..4ccd7786 100644 --- a/debugging/code-reading/readme.md +++ b/debugging/code-reading/readme.md @@ -16,6 +16,8 @@ Take a look at the following code: ``` Explain why line 5 and line 8 output different numbers. +line 5 logs the "let x" from the local scope of the function +where as line 8 uses the "let x" from line one in the global scope ## Question 2 @@ -34,6 +36,13 @@ console.log(y); ``` What will be the output of this code. Explain your answer in 50 words or less. +you will get +10 +undefined +undefined +the x is in the global scope so accsesable to f1 +f1 has no return value to log +and the y is only in the local scope of the function ## Question 3 diff --git a/hahaha/index.html b/hahaha/index.html new file mode 100644 index 00000000..e562be63 --- /dev/null +++ b/hahaha/index.html @@ -0,0 +1,5 @@ + + + + + \ No newline at end of file diff --git a/hahaha/script.js b/hahaha/script.js new file mode 100644 index 00000000..e69de29b diff --git a/hahaha/style.css b/hahaha/style.css new file mode 100644 index 00000000..e69de29b