Skip to content
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
10 changes: 4 additions & 6 deletions Sprint-1/destructuring/exercise-1/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
28 changes: 28 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,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);
}
});
26 changes: 26 additions & 0 deletions Sprint-1/destructuring/exercise-3/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)}`);
25 changes: 13 additions & 12 deletions debugging/book-library/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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();
}
}
Expand All @@ -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);
Expand All @@ -83,21 +83,22 @@ function render() {
}
changeBut.innerText = readStatus;


changeBut.addEventListener("click", function () {
myLibrary[i].check = !myLibrary[i].check;
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 }
}
8 changes: 8 additions & 0 deletions debugging/code-reading/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

Expand Down Expand Up @@ -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.
3 changes: 1 addition & 2 deletions debugging/exampleEJS/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
2 changes: 1 addition & 1 deletion debugging/exampleEJS/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ function exampleFunction() {
return true
}

export default exampleFunction;
module.export = exampleFunction;

2 changes: 1 addition & 1 deletion debugging/exampleEJS/script.test.js
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand Down