diff --git a/Sprint-1/destructuring/exercise-1/exercise.js b/Sprint-1/destructuring/exercise-1/exercise.js index 1ff2ac5c..d86bc7bf 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..4c67324b 100644 --- a/Sprint-1/destructuring/exercise-2/exercise.js +++ b/Sprint-1/destructuring/exercise-2/exercise.js @@ -70,3 +70,16 @@ let hogwarts = [ occupation: "Teacher", }, ]; +// Task 1 +hogwarts.forEach(({ firstName, lastName, house }) => { + if (house === "Gryffindor") { + console.log(firstName, lastName); + } +}); + +// Task 2 +hogwarts.forEach(({ firstName, lastName, occupation, pet }) => { + if (occupation === "Teacher" && pet !== null) { + console.log(firstName, lastName); + } +}); diff --git a/Sprint-1/destructuring/exercise-3/exercise.js b/Sprint-1/destructuring/exercise-3/exercise.js index b3a36f4e..14328b99 100644 --- a/Sprint-1/destructuring/exercise-3/exercise.js +++ b/Sprint-1/destructuring/exercise-3/exercise.js @@ -6,3 +6,17 @@ let order = [ { itemName: "Hot Coffee", quantity: 2, unitPricePence: 100 }, { itemName: "Hash Brown", quantity: 4, unitPricePence: 40 }, ]; +let totalPence = 0; + +console.log("QTY ITEM TOTAL"); + +order.forEach(({ itemName, quantity, unitPricePence }) => { + let itemTotal = quantity * unitPricePence; + totalPence += itemTotal; + + console.log( + `${quantity} ${itemName} ${(itemTotal / 100).toFixed(2)}` + ); +}); + +console.log(`\nTotal: ${(totalPence / 100).toFixed(2)}`); diff --git a/challenges/challenge-cowsay-two/solution1.js b/challenges/challenge-cowsay-two/solution1.js index a7f2416b..fa799814 100644 --- a/challenges/challenge-cowsay-two/solution1.js +++ b/challenges/challenge-cowsay-two/solution1.js @@ -1,30 +1,27 @@ // ================= -// Stripped down cowsayer CLI, +// Stripped down cowsayer CLI, // no libraries // https://nodejs.dev/learn/nodejs-accept-arguments-from-the-command-line // ================= -// 1. Accept arguments - -// how will you accept arguments? - -// 2. Make supplies for our speech bubble - -let topLine = '_'; -let bottomLine = '-'; -let saying = ''; +let topLine = "_"; +let bottomLine = "-"; +let saying = process.argv[2] || "Mooooo"; // 3. Make a cow that takes a string function cowsay(saying) { -// how will you make the speech bubble contain the text? - -// where will the cow picture go? - -// how will you account for the parameter being empty? - + let line = "_".repeat(saying.length + 2); + let bottom = "-".repeat(saying.length + 2); + + console.log(` ${line} `); + console.log(`< ${saying} >`); + console.log(` ${bottom} `); + console.log(` \\ ^__^`); + console.log(` \\ (oo)\\_______`); + console.log(` (__)\\ )\\/\\`); + console.log(` ||----w |`); + console.log(` || ||`); } -//4. Pipe argument into cowsay function and return a cow - -// how will you log this to the console? +cowsay(saying); diff --git a/challenges/challenge-cowsay-two/solution2.js b/challenges/challenge-cowsay-two/solution2.js index 8aca8572..6050f9e2 100644 --- a/challenges/challenge-cowsay-two/solution2.js +++ b/challenges/challenge-cowsay-two/solution2.js @@ -1,18 +1,23 @@ -// ================= -// Stripped down cowsayer CLI, -// no libraries or arguments -// https://nodejs.dev/learn/accept-input-from-the-command-line-in-nodejs -// ================= - -// 1. Make a command line interface. - -// 2. Make supplies for our speech bubble - -// 3. Make a cow that takes a string +const readline = require("readline"); +const rl = readline.createInterface({ + input: process.stdin, + output: process.stdout, +}); const cow = (saying) => { - // how did you make the cow before? -} + let line = "_".repeat(saying.length + 2); + let bottom = "-".repeat(saying.length + 2); -// 4. Use readline to get a string from the terminal -// (with a prompt so it's clearer what we want) \ No newline at end of file + console.log(` ${line} `); + console.log(`< ${saying} >`); + console.log(` ${bottom} `); + console.log(` \\ ^__^`); + console.log(` \\ (oo)\\_______`); + console.log(` (__)\\ )\\/\\`); + console.log(` ||----w |`); + console.log(` || ||`); +}; +rl.question("What would you like the cow to say? ", function (answer) { + cow(answer || "Mooooo"); + rl.close(); +}); diff --git a/challenges/challenge-weather-app/assets/styles.css b/challenges/challenge-weather-app/assets/styles.css index 9046ba1b..14abf3d5 100644 --- a/challenges/challenge-weather-app/assets/styles.css +++ b/challenges/challenge-weather-app/assets/styles.css @@ -113,7 +113,6 @@ animation: 0.25s forwards fade-in; display: block; - opacity: 0; object-fit: cover; flex: 0 0 auto; @@ -125,7 +124,6 @@ background: rgba(0, 0, 0, 0.5); } - /* Credits ------------------------------------------------------------------------------*/ .info { diff --git a/challenges/challenge-weather-app/index.html b/challenges/challenge-weather-app/index.html index b1add346..2815a717 100644 --- a/challenges/challenge-weather-app/index.html +++ b/challenges/challenge-weather-app/index.html @@ -1,49 +1,53 @@ - - - - - - - Meteoropolis - - - - - - -
-
-

- Meteor - opolis -

-
- -
- -
-

-

- - on - Unsplash -

-
- -
- -
- -
-
- - - - + + + + + + Meteoropolis + + + + + + +
+
+

+ Meteor + opolis +

+
+ +
+ +
+

+

+ + on + Unsplash +

+
+ +
+ +
+ +
+
+ + + diff --git a/challenges/unit-testing/katas-tdd/roman-numerals/convert-to-new-roman.js b/challenges/unit-testing/katas-tdd/roman-numerals/convert-to-new-roman.js index 0f2fe4b5..49dccbe0 100644 --- a/challenges/unit-testing/katas-tdd/roman-numerals/convert-to-new-roman.js +++ b/challenges/unit-testing/katas-tdd/roman-numerals/convert-to-new-roman.js @@ -1,3 +1,29 @@ -function convertToNewRoman(n) {} +function convertToNewRoman(n) { + const romanNumerals = [ + [1000, "M"], + [900, "CM"], + [500, "D"], + [400, "CD"], + [100, "C"], + [90, "XC"], + [50, "L"], + [40, "XL"], + [10, "X"], + [9, "IX"], + [5, "V"], + [4, "IV"], + [1, "I"], + ]; + let result = ""; + + for (let [value, symbol] of romanNumerals) { + while (n >= value) { + result += symbol; + n -= value; + } + } + + return result; +} module.exports = convertToNewRoman; diff --git a/challenges/unit-testing/katas-tdd/roman-numerals/convert-to-new-roman.test.js b/challenges/unit-testing/katas-tdd/roman-numerals/convert-to-new-roman.test.js index ae49f737..d7862a28 100644 --- a/challenges/unit-testing/katas-tdd/roman-numerals/convert-to-new-roman.test.js +++ b/challenges/unit-testing/katas-tdd/roman-numerals/convert-to-new-roman.test.js @@ -2,6 +2,11 @@ let convertToNewRoman = require("./convert-to-new-roman"); test("returns I if passed 1 as an argument", function () { // Arrange + const input = 1; + // Act + const result = convertToNewRoman(input); + // Assert + expect(result).toBe("I"); }); diff --git a/challenges/unit-testing/katas-tdd/roman-numerals/convert-to-old-roman.js b/challenges/unit-testing/katas-tdd/roman-numerals/convert-to-old-roman.js index f7f0d06d..045e2f46 100644 --- a/challenges/unit-testing/katas-tdd/roman-numerals/convert-to-old-roman.js +++ b/challenges/unit-testing/katas-tdd/roman-numerals/convert-to-old-roman.js @@ -1,3 +1,5 @@ -function convertToOldRoman(n) {} +function convertToOldRoman(n) { + return "I"; +} module.exports = convertToOldRoman; diff --git a/challenges/unit-testing/katas-tdd/roman-numerals/convert-to-old-roman.test.js b/challenges/unit-testing/katas-tdd/roman-numerals/convert-to-old-roman.test.js index b9a43869..e7a02f34 100644 --- a/challenges/unit-testing/katas-tdd/roman-numerals/convert-to-old-roman.test.js +++ b/challenges/unit-testing/katas-tdd/roman-numerals/convert-to-old-roman.test.js @@ -2,6 +2,11 @@ let convertToOldRoman = require("./convert-to-old-roman"); test("returns I if passed 1 as an argument", function () { // Arrange + const input = 1; + // Act + const result = convertToOldRoman(input); + // Assert + expect(result).toBe("I"); }); diff --git a/challenges/unit-testing/passing-tests/car-sales/car-sales.js b/challenges/unit-testing/passing-tests/car-sales/car-sales.js index e8bb8511..18d20e99 100644 --- a/challenges/unit-testing/passing-tests/car-sales/car-sales.js +++ b/challenges/unit-testing/passing-tests/car-sales/car-sales.js @@ -1,3 +1,15 @@ -function sales(carsSold) {} +function sales(carsSold) { + let totals = {}; + + for (let car of carsSold) { + if (totals[car.make]) { + totals[car.make] += car.price; + } else { + totals[car.make] = car.price; + } + } + + return totals; +} module.exports = sales; diff --git a/challenges/unit-testing/passing-tests/factorial/factorial.js b/challenges/unit-testing/passing-tests/factorial/factorial.js index fefa4368..c8e2ef59 100644 --- a/challenges/unit-testing/passing-tests/factorial/factorial.js +++ b/challenges/unit-testing/passing-tests/factorial/factorial.js @@ -1,13 +1,11 @@ -// int is an integer -// a factorial is the product of all non-negative integers -// less than or equal to the iniital number. +function factorial(int) { + let result = 1; -// for example the factorial of 5 is 120 -// 120 = 1 * 2 * 3 * 4 * 5 + for (let i = 1; i <= int; i++) { + result *= i; + } -// calculate and return the factorial of int -// note: factorial of 0 is 1 - -function factorial(int) {} + return result; +} module.exports = factorial; diff --git a/challenges/unit-testing/passing-tests/get-average/get-average.js b/challenges/unit-testing/passing-tests/get-average/get-average.js index b5588a57..ce20599e 100644 --- a/challenges/unit-testing/passing-tests/get-average/get-average.js +++ b/challenges/unit-testing/passing-tests/get-average/get-average.js @@ -2,6 +2,18 @@ // return the average of all the numbers // be sure to exclude the strings -function average(numbers) {} +function average(numbers) { + let sum = 0; + let count = 0; + + for (let item of numbers) { + if (typeof item === "number") { + sum += item; + count++; + } + } + + return sum / count; +} module.exports = average; diff --git a/challenges/unit-testing/writing-tests/largest-number/largest-number.test.js b/challenges/unit-testing/writing-tests/largest-number/largest-number.test.js index ce674209..1768759a 100644 --- a/challenges/unit-testing/writing-tests/largest-number/largest-number.test.js +++ b/challenges/unit-testing/writing-tests/largest-number/largest-number.test.js @@ -2,12 +2,13 @@ let getLargestNumber = require("./largest-number"); test("returns largest number in array", function () { // Arrange + let input = [3, 21, 88, 4, 36]; + let original = [...input]; + // Act + let result = getLargestNumber(input); + // Assert + expect(result).toBe(88); + expect(input).toEqual(original); }); - -// example -// input: [3, 21, 88, 4, 36]; -// expected: 88; - -// also test that the original array hasn't changed diff --git a/challenges/unit-testing/writing-tests/remove-vowels/remove-vowels-in-array.js b/challenges/unit-testing/writing-tests/remove-vowels/remove-vowels-in-array.js index 6f4e06bf..f609e3d6 100644 --- a/challenges/unit-testing/writing-tests/remove-vowels/remove-vowels-in-array.js +++ b/challenges/unit-testing/writing-tests/remove-vowels/remove-vowels-in-array.js @@ -9,8 +9,3 @@ function removeVowelsFromWords(words) { } module.exports = removeVowelsFromWords; - -/* - input: ["Irina", "Etza", "Daniel"] - expected output: ["rn", "tz", "Dnl"] -*/ diff --git a/challenges/unit-testing/writing-tests/remove-vowels/remove-vowels-in-array.test.js b/challenges/unit-testing/writing-tests/remove-vowels/remove-vowels-in-array.test.js index ee739e22..3e59add8 100644 --- a/challenges/unit-testing/writing-tests/remove-vowels/remove-vowels-in-array.test.js +++ b/challenges/unit-testing/writing-tests/remove-vowels/remove-vowels-in-array.test.js @@ -2,10 +2,12 @@ let removeVowelsFromWords = require("./remove-vowels-in-array"); test("remove vowels from all words in array", function () { // Arrange + let input = ["Irina", "Etza", "Daniel"]; + let expected = ["rn", "tz", "Dnl"]; + // Act + let result = removeVowelsFromWords(input); + // Assert + expect(result).toEqual(expected); }); - -// example -// input: ["Irina", "Etza", "Daniel"] -// expected output: ["rn", "tz", "Dnl"] diff --git a/challenges/unit-testing/writing-tests/remove-vowels/remove-vowels.js b/challenges/unit-testing/writing-tests/remove-vowels/remove-vowels.js index e5bb67ba..346ec953 100644 --- a/challenges/unit-testing/writing-tests/remove-vowels/remove-vowels.js +++ b/challenges/unit-testing/writing-tests/remove-vowels/remove-vowels.js @@ -18,12 +18,4 @@ function removeVowels(word) { return result.join(""); } -module.exports = removeVowels; - -/* - Let's trace this piece of code - what is the value of result with this input - - let result = removeVowels('samuel'); - - what is the value of result? -*/ +module.exports = removeVowels("samuel"); diff --git a/debugging/book-library/index.html b/debugging/book-library/index.html index 23acfa71..de831da7 100644 --- a/debugging/book-library/index.html +++ b/debugging/book-library/index.html @@ -1,12 +1,9 @@ - + - - + My Book Library + + @@ -31,7 +28,7 @@

Library

Library /> add the new book (object in array) -//via Book function and start render function function submit() { - if ( - title.value == null || - title.value == "" || - pages.value == null || - pages.value == "" - ) { - alert("Please fill all fields!"); - return false; - } else { - let book = new Book(title.value, title.value, pages.value, check.checked); - library.push(book); - render(); + const title = titleInput.value.trim(); + const author = authorInput.value.trim(); + const pages = Number(pagesInput.value); + + if (!title || !author || !pages || pages <= 0) { + alert("Please fill all fields correctly!"); + return; } + + const book = new Book(title, author, pages, readCheckbox.checked); + + myLibrary.push(book); + render(); } function Book(title, author, pages, check) { @@ -51,53 +50,50 @@ function Book(title, author, pages, check) { } function render() { - let table = document.getElementById("display"); - let rowsNumber = table.rows.length; - //delete old table - for (let n = rowsNumber - 1; n > 0; n-- { - table.deleteRow(n); + const table = document.getElementById("display"); + + // remove old rows (keep header row) + while (table.rows.length > 1) { + table.deleteRow(1); } - //insert updated row and cells - 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); - let pagesCell = row.insertCell(2); - let wasReadCell = row.insertCell(3); - let deleteCell = row.insertCell(4); - titleCell.innerHTML = myLibrary[i].title; - authorCell.innerHTML = myLibrary[i].author; - pagesCell.innerHTML = myLibrary[i].pages; - - //add and wait for action for read/unread button - let changeBut = document.createElement("button"); - changeBut.id = i; - changeBut.className = "btn btn-success"; - wasReadCell.appendChild(changeBut); - let readStatus = ""; - if (myLibrary[i].check == false) { - readStatus = "Yes"; - } else { - readStatus = "No"; - } - changeBut.innerText = readStatus; - - changeBut.addEventListener("click", function () { + + for (let i = 0; i < myLibrary.length; i++) { + const row = table.insertRow(1); + + const titleCell = row.insertCell(0); + const authorCell = row.insertCell(1); + const pagesCell = row.insertCell(2); + const wasReadCell = row.insertCell(3); + const deleteCell = row.insertCell(4); + + titleCell.textContent = myLibrary[i].title; + authorCell.textContent = myLibrary[i].author; + pagesCell.textContent = myLibrary[i].pages; + + const toggleButton = document.createElement("button"); + toggleButton.className = "btn btn-success"; + toggleButton.textContent = myLibrary[i].check ? "Yes" : "No"; + + toggleButton.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 () { - alert(`You've deleted title: ${myLibrary[i].title}`); + wasReadCell.appendChild(toggleButton); + + const deleteButton = document.createElement("button"); + deleteButton.className = "btn btn-warning"; + deleteButton.textContent = "Delete"; + + deleteButton.addEventListener("click", function () { + const deletedTitle = myLibrary[i].title; + myLibrary.splice(i, 1); render(); + + alert(`You've deleted title: ${deletedTitle}`); }); + + deleteCell.appendChild(deleteButton); } }