From f051bbc28afd72dbf80875ab25a6d28773497ce9 Mon Sep 17 00:00:00 2001 From: filkovskyy Date: Tue, 2 May 2023 13:04:08 +0100 Subject: [PATCH] solved exercises --- debugging/demo/demo1/demo1.js | 53 +++++++------ debugging/demo/demo2/index.html | 2 +- debugging/demo/demo2/script.js | 88 +++++++++++----------- debugging/exercises/exercise1/exercise1.js | 20 +++-- debugging/exercises/exercise2/index.html | 2 +- debugging/exercises/exercise2/main.js | 26 ++++--- errors/exercise1.js | 2 +- errors/exercise2.js | 11 +-- errors/exercise3.js | 2 +- errors/exercise4.js | 6 +- errors/exercise5.js | 4 +- errors/exercise6.js | 8 +- errors/exercise7.js | 34 ++++----- 13 files changed, 136 insertions(+), 122 deletions(-) diff --git a/debugging/demo/demo1/demo1.js b/debugging/demo/demo1/demo1.js index 0d8e695..ecf5b21 100644 --- a/debugging/demo/demo1/demo1.js +++ b/debugging/demo/demo1/demo1.js @@ -1,35 +1,42 @@ -const tomatoes = 'tomatoes'; -const chocolate= 'chocolate'yummy'; -const yogurt = 'yogurt'; +const tomatoes = "tomatoes"; +const chocolate = "chocolate"; +const yogurt = "yogurt"; const rice = "rice"; -const fridge =[{ item: tomatoes, quantity: 5}, - { item: chocolate, quantity: 1}, - { item: yogurt, quantity: 5}]; +const fridge = [ + { item: tomatoes, quantity: 5 }, + { item: chocolate, quantity: 1 }, + { item: yogurt, quantity: 5 }, +]; -const wantedList = [{ item: tomatoes, quantity: 4}, - { item: chocolate, quantity: 10}, - { item: yogurt, quantity: 2}] +const wantedList = [ + { item: tomatoes, quantity: 4 }, + { item: chocolate, quantity: 10 }, + { item: yogurt, quantity: 2 }, +]; const shoppingList = (fridge, wantedList) => { - return wantedList.map(groceryWantedList => { - let groceryFridge = fridge.find( gf => gf.item === groceryWantedList.item); - if (groceryFridge===null){ + return wantedList + .map((groceryWantedList) => { + let groceryFridge = fridge.find( + (gf) => gf.item === groceryWantedList.item + ); + if (groceryFridge === null) { return groceryWantedList; } else { - if (groceryFridge.quantity < groceryWantedList.quantity){ + if (groceryFridge.quantity < groceryWantedList.quantity) { return { - item: groceryWantedList.item, - quantity: groceryWantedList.quantity - groceryFridge.quantity + item: groceryWantedList.item, + quantity: groceryWantedList.quantity - groceryFridge.quantity, }; - } - else{ - return null; + } else { + return null; + } } - } - }).filter(item => item !== null); + }) + .filter((item) => item !== null); }; -shoppingList (fridge, wantedList).forEach( (item) => { - console.log (`${item.item}: ${item.quantity}`); - }) \ No newline at end of file +shoppingList(fridge, wantedList).forEach((item) => { + console.log(`${item.item}: ${item.quantity}`); +}); diff --git a/debugging/demo/demo2/index.html b/debugging/demo/demo2/index.html index 8cc1a42..9b90efb 100644 --- a/debugging/demo/demo2/index.html +++ b/debugging/demo/demo2/index.html @@ -1,6 +1,6 @@ - + { - var data = [ - { - text: 'Overshadowing #UNGA is the big question: Will Obama and Rouhani meet?', - href: 'https://twitter.com/cnnbrk/status/382528782738800641' - }, - { - text: "Marine's family hopes visiting Iranian president will help free their son", - href: 'https://twitter.com/cnnbrk/status/382519683053649920' - }, - { - text: 'Obama addresses United Nations.', - href: 'https://twitter.com/cnnbrk/status/382507500903202817' - }, - { - text: '', - href: 'https://twitter.com/CNNMoney/status/382497891723804672' - }, - { - text: "If you're seeing this as a button, congratulations!", - href: 'http://twitter.com' - } - ]; - for (var i = 0; i { + var data = [ + { + text: "Overshadowing #UNGA is the big question: Will Obama and Rouhani meet?", + href: "https://twitter.com/cnnbrk/status/382528782738800641", + }, + { + text: "Marine's family hopes visiting Iranian president will help free their son", + href: "https://twitter.com/cnnbrk/status/382519683053649920", + }, + { + text: "Obama addresses United Nations.", + href: "https://twitter.com/cnnbrk/status/382507500903202817", + }, + { + text: "", + href: "https://twitter.com/CNNMoney/status/382497891723804672", + }, + { + text: "If you're seeing this as a button, congratulations!", + href: "http://twitter.com", + }, + ]; - buttons.forEach(el => el.addEventListener('click', evt => { - const ctrl = evt.target; - if (!ctrl.getAttribute('data-href')) { - document.location = ctrl.getAttribute('data-href'); - }})) - }) + for (var i = 0; i < data.length; i++) { + if (data[i].text) { + const pElement = document.createElement("p"); + const button = document.createElement("button"); + document.querySelector("#news").appendChild(pElement); + pElement.appendChild(button); + button.type = "button"; + button.classList.add(["btn", "btn-default"]); + button.setAttribute("data-href", data[i].href); + button.innerText = data[i].text; + } + } + const buttons = [...document.querySelectorAll("button")]; + + buttons.forEach((el) => + el.addEventListener("click", (evt) => { + const ctrl = evt.target; + if (ctrl.getAttribute("data-href")) { + document.location = ctrl.getAttribute("data-href"); + } + }) + ); +}); diff --git a/debugging/exercises/exercise1/exercise1.js b/debugging/exercises/exercise1/exercise1.js index 9878e8c..f3aca5e 100644 --- a/debugging/exercises/exercise1/exercise1.js +++ b/debugging/exercises/exercise1/exercise1.js @@ -8,29 +8,27 @@ const countDown = () => { console.log(secondsTolaunch--); switch (secondsTolaunch) { case 7: - console.log('Close Davy Jones' Locker..'); + console.log("'Close Davy Jones' Locker..'"); break; case 3: - console.log('Ignition...'); + console.log("Ignition..."); break; case 0: - console.log('Liftoff!'); + console.log("Liftoff!"); clearInterval(interval); break; default: break; } - }; - if (fuelLevel >= 20000) { - console.log(('Fuel level cleared.'); - launchReady = true; + console.log("Fuel level cleared."); + launchReady = true; } else { - console.log('WARNING: Insufficient fuel!'); - launchReady = false; + console.log("WARNING: Insufficient fuel!"); + launchReady = false; } -if (launchReady){ - interval = setInterval(countDown, 1000) +if (launchReady) { + interval = setInterval(countDown, 1000); } diff --git a/debugging/exercises/exercise2/index.html b/debugging/exercises/exercise2/index.html index 3ab2800..4255bdd 100644 --- a/debugging/exercises/exercise2/index.html +++ b/debugging/exercises/exercise2/index.html @@ -77,6 +77,6 @@

Odin's Library

- + diff --git a/debugging/exercises/exercise2/main.js b/debugging/exercises/exercise2/main.js index dc6b8ee..8bffdc7 100644 --- a/debugging/exercises/exercise2/main.js +++ b/debugging/exercises/exercise2/main.js @@ -19,7 +19,7 @@ const book2 = { read: "No", }; -myLibrary.push(book1; +myLibrary.push(book1); myLibrary.push(book2); render(); @@ -28,15 +28,15 @@ addButtons.forEach((button) => { button.addEventListener("click", () => { formContainer.style.display = "block"; }); -}; +}); function addDeleteButtons() { let deleteButtons = document.querySelectorAll(".delete"); deleteButtons.forEach((button) => { - if (button.getAttribute("data-book") == bookNumber) { + if (button.getAttribute("data-book") === bookNumber.toString()) { //Only add eventListeners to new books - button.addEventListener("clicksss", () => { + button.addEventListener("click", () => { deleteBook(button.getAttribute("data-book")); }); } @@ -47,7 +47,7 @@ function addReadButtons() { let readButtons = document.querySelectorAll(".change-read"); readButtons.forEach((button) => { - if (button.getAttribute("data-book") == bookNumber) { + if (button.getAttribute("data-book") === bookNumber.toString()) { button.addEventListener("click", () => { changeReadStatus(button.getAttribute("data-book"), button); }); @@ -61,13 +61,14 @@ function deleteBook(number) { } function changeReadStatus(number, button) { - if (myLibrary[number]["read"] === "Yes") { - myLibrary[number]["read"] = "No"; + let index = parseInt(number); + if (myLibrary[index]["read"] === "Yes") { + myLibrary[index]["read"] = "No"; button.innerText = "No"; button.classList.remove("button-green"); button.classList.add("button-red"); } else { - myLibrary[number]["read"] = "Yes"; + myLibrary[index]["read"] = "Yes"; button.innerText = "Yes"; button.classList.remove("button-red"); button.classList.add("button-green"); @@ -80,6 +81,9 @@ function addBookToLibrary(title, author, pages, read) { } function render() { + addReadButtons(); + addDeleteButtons(); + for (let i = 0; i < myLibrary.length; i++) { if (i === bookNumber) { let row = document.createElement("tr"); @@ -93,7 +97,7 @@ function render() { let titleCell = document.createElement("td"); titleCell.append(myLibrary[i].title); - row.append(titleCella); + row.append(titleCell); let authorCell = document.createElement("td"); authorCell.append(myLibrary[i].author); @@ -120,7 +124,7 @@ function render() { row.append(readCell); let deleteCell = document.createElement("td"); - let deleteB = document.createElement("button"); + let deleteButton = document.createElement("button"); let icon = document.createElement("ion-icon"); icon.setAttribute("name", "trash-outline"); deleteButton.classList.add("delete"); @@ -133,7 +137,7 @@ function render() { tableBody.insertBefore(row, tableBody.firstChild); - addDeletedButtons(); + addDeleteButtons(); addReadButtons(); bookNumber++; diff --git a/errors/exercise1.js b/errors/exercise1.js index 6adfa43..cbfec4d 100644 --- a/errors/exercise1.js +++ b/errors/exercise1.js @@ -1,3 +1,3 @@ -if (3 > Math.PI { +if (3 > Math.PI ){ console.log("wait what?"); } diff --git a/errors/exercise2.js b/errors/exercise2.js index da8708f..bc748c8 100644 --- a/errors/exercise2.js +++ b/errors/exercise2.js @@ -1,6 +1,7 @@ -let charge = function() { - if (sunny) { - useSolarCells(); - } else { - promptBikeRide(); +let charge = function () { + if (sunny) { + useSolarCells(); + } else { + promptBikeRide(); + } }; diff --git a/errors/exercise3.js b/errors/exercise3.js index d100040..f9fd46b 100644 --- a/errors/exercise3.js +++ b/errors/exercise3.js @@ -1,2 +1,2 @@ -let ward = "hello"; +let word = "hello"; word.substring(1); diff --git a/errors/exercise4.js b/errors/exercise4.js index c59741a..f9e7dba 100644 --- a/errors/exercise4.js +++ b/errors/exercise4.js @@ -1,5 +1,5 @@ let numbers = { a: 13, b: 37, c: 42 }; -numbers.map(function (num) { - return num * 2; -}); +for (let key in numbers) { + numbers[key] = numbers[key] * 2; +} diff --git a/errors/exercise5.js b/errors/exercise5.js index db8a957..1a7d41d 100644 --- a/errors/exercise5.js +++ b/errors/exercise5.js @@ -1,2 +1,2 @@ -let name; -name.substring(1); +let name = "name"; +let firstLetter = name.substring(1); diff --git a/errors/exercise6.js b/errors/exercise6.js index 8ef8108..f6210b5 100644 --- a/errors/exercise6.js +++ b/errors/exercise6.js @@ -3,8 +3,8 @@ // Item #2 on the list is eggs // Item #3 on the list is milk -let arr ["bread", eggs", "milk"]; +let arr = ["bread", "eggs", "milk"]; -items.forEach(item, index -> { - console.log(`Item #${index + 1} on the list is ${item}`); -}; +arr.forEach((item, index) => { + console.log(`Item #${index + 1} on the list is ${item}`); +}); diff --git a/errors/exercise7.js b/errors/exercise7.js index 40633fd..fd21453 100644 --- a/errors/exercise7.js +++ b/errors/exercise7.js @@ -6,27 +6,27 @@ // ] function getTemperatureReport(cities) { - let report = []; + let report = []; - for(let i = 0; i < cities.length(); i++) { - let temperature = temperatureService(city[i]); - report.push(`The temperature in ${city[i]} is ${temperature} degrees`); - } - return report; + for (let i = 0; i < cities.length; i++) { + let temperature = temperatureService(cities[i]); + report.push(`The temperature in ${cities[i]} is ${temperature} degrees`); + } + return report; } -function temperatureService() { - let temparatureMap = { - 'London': 10, - 'Paris': 12, - 'Barcelona': 17, - 'Dubai' 27, - 'Mumbai': 29, - 'São Paulo': 23 - 'Lagos': 33 - +function temperatureService(city) { + let temparatureMap = { + London: 10, + Paris: 12, + Barcelona: 17, + Dubai: 27, + Mumbai: 29, + "São Paulo": 23, + Lagos: 33, + }; - return temparatureMap[city]; + return temparatureMap[city]; } let report = getTemperatureReport(["London", "Paris", "São Paulo"]);