diff --git a/Sprint-1/destructuring/exercise-1/exercise.js b/Sprint-1/destructuring/exercise-1/exercise.js index 1ff2ac5c..5d639768 100644 --- a/Sprint-1/destructuring/exercise-1/exercise.js +++ b/Sprint-1/destructuring/exercise-1/exercise.js @@ -3,13 +3,11 @@ const personOne = { age: 34, favouriteFood: "Spinach", }; - -// 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}.` ); } - -introduceYourself(personOne); +// I add this line to check if the function is being called +console.log("Script is running and function is being called!"); +introduceYourself(personOne); \ No newline at end of file diff --git a/Sprint-1/destructuring/exercise-2/exercise.js b/Sprint-1/destructuring/exercise-2/exercise.js index e11b75eb..d0824349 100644 --- a/Sprint-1/destructuring/exercise-2/exercise.js +++ b/Sprint-1/destructuring/exercise-2/exercise.js @@ -70,3 +70,31 @@ let hogwarts = [ occupation: "Teacher", }, ]; + +// Loop through each character in the hogwarts array. I can use forEach() +// to loop through each item: + + +hogwarts.forEach(function(character) { + // Destructure - extract firstName, lastName, and house from current character object + const { firstName, lastName, house } = character; + + // Check if this character belongs to Gryffindor house + if (house === "Gryffindor") { + // If yes, print their full name + console.log(firstName + " " + lastName); + } +}); + + +// Loop through each character in the hogwarts array +hogwarts.forEach(function(character) { + // Destructure - extract firstName, lastName, occupation, and pet from current character + const { firstName, lastName, occupation, pet } = character; + + // Check if this person is a teacher AND has a pet (pet is not null) + if (occupation === "Teacher" && pet !== null) { + // If both conditions are true, print their name. + console.log(firstName + " " + lastName); + } +}); \ No newline at end of file diff --git a/Sprint-1/destructuring/exercise-3/exercise.js b/Sprint-1/destructuring/exercise-3/exercise.js index b3a36f4e..e537bf53 100644 --- a/Sprint-1/destructuring/exercise-3/exercise.js +++ b/Sprint-1/destructuring/exercise-3/exercise.js @@ -6,3 +6,29 @@ let order = [ { itemName: "Hot Coffee", quantity: 2, unitPricePence: 100 }, { itemName: "Hash Brown", quantity: 4, unitPricePence: 40 }, ]; + + +// Print receipt header +console.log("QTY ITEM TOTAL"); + +let totalCost = 0; + +// Loop through each item in the order +order.forEach(function(item) { + // Destructure - extract the properties we need from each item + const { itemName, quantity, unitPricePence } = item; + + // Calculate total for this item (convert pence to pounds) + const itemTotal = (quantity * unitPricePence) / 100; + + // Add to overall total + totalCost += itemTotal; + + // Format and print the line with proper spacing + //.padEnd give a block of spaces so this way it keeps the values + // of total consistently far from it. from beginng to end it is 18 + console.log(`${quantity} ${itemName.padEnd(18)} ${itemTotal.toFixed(2)}`); +}); + +// Print the final total +console.log(`\nTotal: ${totalCost.toFixed(2)}`); \ No newline at end of file diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index 75ce6c1d..7cf84217 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -7,7 +7,7 @@ window.addEventListener("load", function (e) { function populateStorage() { if (myLibrary.length == 0) { - let book1 = new Book("Robison Crusoe", "Daniel Defoe", "252", true); + let book1 = new Book("Robinson Crusoe", "Daniel Defoe", "252", true); let book2 = new Book( "The Old Man and the Sea", "Ernest Hemingway", @@ -37,8 +37,8 @@ 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); + let book = new Book(title.value, author.value, pages.value, check.checked); // Fixed: was title.value twice + myLibrary.push(book); // Fixed: was library instead of myLibrary render(); } } @@ -54,12 +54,12 @@ 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++) { + for (let i = 0; i < length; i++) { // Added missing { let row = table.insertRow(1); let titleCell = row.insertCell(0); let authorCell = row.insertCell(1); @@ -83,6 +83,7 @@ function render() { } changeBut.innerText = readStatus; + changeBut.addEventListener("click", function () { myLibrary[i].check = !myLibrary[i].check; render(); @@ -90,14 +91,14 @@ function render() { //add delete button to every row and render again 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"; + delButton.addEventListener("click", function () { alert(`You've deleted title: ${myLibrary[i].title}`); myLibrary.splice(i, 1); render(); }); - } -} + } // Added missing } +} \ No newline at end of file diff --git a/debugging/code-reading/readme.md b/debugging/code-reading/readme.md index 4090c14c..7491ec21 100644 --- a/debugging/code-reading/readme.md +++ b/debugging/code-reading/readme.md @@ -16,6 +16,7 @@ Take a look at the following code: ``` Explain why line 5 and line 8 output different numbers. +Because inside the code, x is declared globally. so it is using the global variable. while in line 8 it logs the variable from the function. ## Question 2 @@ -34,6 +35,10 @@ console.log(y); ``` What will be the output of this code. Explain your answer in 50 words or less. +10 +20 +because inside the function y is declared but not called. +i checked the internet. it says results will be undefined because y is defined inside teh function scoop. ## Question 3 @@ -62,3 +67,6 @@ console.log(y); ``` What will be the output of this code. Explain your answer in 50 words or less. +9 +{x: 10} +x is a primitive (number) passed by value - modifying it inside f1() doesn't affect the original. y is an object passed by reference - modifying its property inside f2() changes the original object. \ No newline at end of file diff --git a/debugging/exampleEJS/package.json b/debugging/exampleEJS/package.json index 8d194c7a..e08e635e 100644 --- a/debugging/exampleEJS/package.json +++ b/debugging/exampleEJS/package.json @@ -5,8 +5,7 @@ "main": "script.mjs", "type": "module", "scripts": { - "test": "node --experimental-vm-modules node_modules/jest/bin/jest.js" - }, + "test": "jest"}, "dependencies": { "jest": "^29.7.0" }, diff --git a/debugging/exampleEJS/script.js b/debugging/exampleEJS/script.js index 14b70bc1..20794e95 100644 --- a/debugging/exampleEJS/script.js +++ b/debugging/exampleEJS/script.js @@ -3,5 +3,5 @@ function exampleFunction() { return true } -export default exampleFunction; +module.export = exampleFunction; diff --git a/debugging/exampleEJS/script.test.js b/debugging/exampleEJS/script.test.js index 4f0d79bd..cc6b11c6 100644 --- a/debugging/exampleEJS/script.test.js +++ b/debugging/exampleEJS/script.test.js @@ -1,5 +1,5 @@ //check import works -import exampleFunction from './script'; +const exampleFunction = require('./script.js'); // check these functions exist describe('Basic test', () => { test('exampleFunction exists', () => {