diff --git a/InClass/Callbacks/exercise-1/exercise.js b/InClass/Callbacks/exercise-1/exercise.js index 40f06b0..387ff68 100644 --- a/InClass/Callbacks/exercise-1/exercise.js +++ b/InClass/Callbacks/exercise-1/exercise.js @@ -10,4 +10,15 @@ Update your code to make the colour change every 5 seconds to something differen Prefer to work on a codepen? https://codepen.io/makanti/pen/abOreLg ================ -*/ \ No newline at end of file +*/ + +// Task 1 +// setTimeout(function () { +// document.querySelector("#main").style.backgroundColor = 'red'; +// }, 5000); + +// Task 2 +setInterval(function () { + let randomColor = Math.floor(Math.random()*16777215).toString(16); + document.querySelector("#main").style.backgroundColor = `#${randomColor}`; +}, 5000); \ No newline at end of file diff --git a/InClass/Callbacks/exercise-2/exercise.js b/InClass/Callbacks/exercise-2/exercise.js index eca9595..c4e472a 100644 --- a/InClass/Callbacks/exercise-2/exercise.js +++ b/InClass/Callbacks/exercise-2/exercise.js @@ -63,8 +63,66 @@ const movies = [ // create showMovies function +// function showMovies(movies) { +// let i = 0; +// document.querySelector("#movies-number").innerText = movies.length; +// setInterval(function () { +// if (i < movies.length) { +// let paragraph = document.createElement("p"); +// paragraph.innerHTML = `"${movies[i].title}" by ${movies[i].director}` +// document.querySelector("#all-movies").appendChild(paragraph); +// } +// else return + +// i++; +// }, 1000); +// } + +function showMovies(movies) { + document.querySelector("#movies-number").innerText = movies.length; + document.querySelectorAll('.movie-item').forEach(e => e.remove()); + for (let i = 0; i < movies.length; i++) { + setTimeout(function () { + let paragraph = document.createElement("p"); + paragraph.className = "movie-item"; + paragraph.innerHTML = `"${movies[i].title}" by ${movies[i].director}` + document.querySelector("#all-movies").appendChild(paragraph); + }, 1000 + (1000 * i)); + } +} // create a new movie object for your favorite movie +let newMovie = { + title: "The Lord of The Rings", + director: "Peter Jackson", + type: "fantasy", + haveWatched: true, +} // create addMovies function + +function addMovie(movie, movies) { + setTimeout(function () { + movies.push(movie); + showMovies(movies); + //document.querySelector("#movies-number").innerText = movies.length; + }, 2000); +} + +showMovies(movies); + +// update list of movies when form is POSTed +let saveBtn = document.querySelector("#saveBtn"); + +saveBtn.addEventListener("click", function (event) { + event.preventDefault(); + let newMovie = { + title: document.querySelector("#title").value, + director: document.querySelector("#director").value, + type: document.querySelector("#type"), + haveWatched: document.querySelector("#watched").value + } + + addMovie(newMovie, movies); +}); \ No newline at end of file diff --git a/InClass/Callbacks/exercise-2/index.html b/InClass/Callbacks/exercise-2/index.html index bc9654c..45f0501 100644 --- a/InClass/Callbacks/exercise-2/index.html +++ b/InClass/Callbacks/exercise-2/index.html @@ -22,7 +22,18 @@

My movies

Number of movies:

- + +
+ + + + + + + + + +
diff --git a/InClass/DOM-practice/main.js b/InClass/DOM-practice/main.js index be9f609..9ca8bd0 100644 --- a/InClass/DOM-practice/main.js +++ b/InClass/DOM-practice/main.js @@ -4,8 +4,7 @@ console.log("Testing JS file loaded!") // Without changing any of the HTML or CSS, update the
tags so that they have white backgrounds. - - +document.querySelectorAll("section").forEach(el => el.style.backgroundColor = "white"); @@ -15,7 +14,7 @@ console.log("Testing JS file loaded!") // Hint: look at the CSS to see if there are any classes already written which you can use. - +document.querySelectorAll("img").forEach(el => el.style.cssText += "display: block; margin-left: auto; margin-right: auto"); @@ -23,3 +22,28 @@ console.log("Testing JS file loaded!") // Task 3 // Google the date of birth and death of each of the people on the page. Without changing any of the HTML or CSS, add this in a paragraph to the end of their
. + +let dates = [ + { + name: "grace-hopper", + born: "9 December 1906", + died: "1 January 1992" + }, + { + name: "katherine-johnson", + born: "26 August 1918", + died: "24 February 2020" + }, + { + name: "ada-lovelace", + born: "10 December 1815", + died: "27 November 1852" + } +] + +for (i of dates) { + let section = document.querySelector(`#${i.name}`).parentElement + let paragraph = document.createElement("p"); + paragraph.innerHTML = `Born on ${i.born} and died on ${i.died}`; + section.insertBefore(paragraph, section.children[section.children.length - 1]); +} \ No newline at end of file diff --git a/mandatory/1-alarmclock/alarmclock.js b/mandatory/1-alarmclock/alarmclock.js index 6ca81cd..ae16965 100644 --- a/mandatory/1-alarmclock/alarmclock.js +++ b/mandatory/1-alarmclock/alarmclock.js @@ -1,4 +1,39 @@ -function setAlarm() {} +let tmr; +let blink; +let myTimer; + +function setAlarm() { + clearInterval(myTimer); + clearInterval(blink); + document.querySelector("body").style.backgroundColor = "white"; + tmr = true; + let input = document.querySelector("#alarmSet").value; + let inputStr = `${String(Math.floor(input / 60)).padStart(2, '0')}:${String(input % 60).padStart(2, '0')}`; + document.querySelector("#timeRemaining").innerHTML = `Time Remaining: ${inputStr}`; + myTimer = setInterval(function () { + if (tmr) { + input--; + } + inputStr = `${String(Math.floor(input / 60)).padStart(2, '0')}:${String(input % 60).padStart(2, '0')}`; + document.querySelector("#timeRemaining").innerHTML = `Time Remaining: ${inputStr}`; + if (input === 0) { + playAlarm(); + let ofs = 0; + blink = setInterval(function(){ + document.querySelector("body").style.backgroundColor = 'rgba(255,0,0,'+Math.abs(Math.sin(ofs))+')'; + ofs += 0.01; + }, 10); + clearInterval(myTimer); + } + }, 1000) +} +document.addEventListener('DOMContentLoaded', function () { + let pauseBtn = document.querySelector("#pause") + pauseBtn.addEventListener("click", () => { + tmr = !tmr; + }); +}); + // DO NOT EDIT BELOW HERE diff --git a/mandatory/1-alarmclock/index.html b/mandatory/1-alarmclock/index.html index ab7d582..5c2dfde 100644 --- a/mandatory/1-alarmclock/index.html +++ b/mandatory/1-alarmclock/index.html @@ -21,7 +21,8 @@

Time Remaining: 00:00

- + +
diff --git a/mandatory/2-quotegenerator/index.html b/mandatory/2-quotegenerator/index.html index b6115be..4ec76bc 100644 --- a/mandatory/2-quotegenerator/index.html +++ b/mandatory/2-quotegenerator/index.html @@ -12,6 +12,16 @@ - + +
+

Click the button to generate a new quote!

+

+ + +

+
diff --git a/mandatory/2-quotegenerator/quotes.js b/mandatory/2-quotegenerator/quotes.js index 39ab245..0fb8fd9 100644 --- a/mandatory/2-quotegenerator/quotes.js +++ b/mandatory/2-quotegenerator/quotes.js @@ -1,3 +1,36 @@ +document.addEventListener('DOMContentLoaded', function () { + let auto = false; + let timer; + + // function to choose and display quote + function chooseQuote() { + let chosenQuote = quotes[Math.floor(Math.random() * quotes.length)]; + document.querySelector("h1").innerHTML = chosenQuote.quote; + document.querySelector("p").innerHTML = chosenQuote.author; + } + + // New quote button + let Btn = document.querySelector("button") + Btn.addEventListener("click", () => { + chooseQuote(); + }); + + // Auto-play button + let toggle = document.querySelector("input"); + toggle.addEventListener("click", () => { + auto = !auto; + if (auto) { + document.querySelector("#autoPlay").innerHTML = "auto-play:ON"; + timer = setInterval(() => { + chooseQuote(); + }, 3000); + } else { + document.querySelector("#autoPlay").innerHTML = ""; + clearInterval(timer); + } + }); +}); + // DO NOT EDIT BELOW HERE // A function which will return one item, at diff --git a/mandatory/2-quotegenerator/style.css b/mandatory/2-quotegenerator/style.css index 63cedf2..bec5d04 100644 --- a/mandatory/2-quotegenerator/style.css +++ b/mandatory/2-quotegenerator/style.css @@ -1 +1,94 @@ /** Write your CSS in here **/ +body { + display: flex; + justify-content: center; + align-items: center; + background-color: orange; +} + +div { + display: flex; + flex-direction: column; + margin: 10rem; + padding: 1.5rem; + background-color: white; + width: 70%; +} + +p { + display: flex; + align-self: flex-end; + height: 1.5rem; +} + +button { + width: 20%; +} + +h1 { + height: 15rem; +} + +/* The switch - the box around the slider */ +.switch { + position: relative; + display: inline-block; + width: 60px; + height: 34px; + margin: 2rem; +} + +/* Hide default HTML checkbox */ +.switch input { + opacity: 0; + width: 0; + height: 0; +} + +/* The slider */ +.slider { + position: absolute; + cursor: pointer; + top: 0; + left: 0; + right: 0; + bottom: 0; + background-color: #ccc; + -webkit-transition: 0.4s; + transition: 0.4s; +} + +.slider:before { + position: absolute; + content: ""; + height: 26px; + width: 26px; + left: 4px; + bottom: 4px; + background-color: white; + -webkit-transition: 0.4s; + transition: 0.4s; +} + +input:checked + .slider { + background-color: #2196f3; +} + +input:focus + .slider { + box-shadow: 0 0 1px #2196f3; +} + +input:checked + .slider:before { + -webkit-transform: translateX(26px); + -ms-transform: translateX(26px); + transform: translateX(26px); +} + +/* Rounded sliders */ +.slider.round { + border-radius: 34px; +} + +.slider.round:before { + border-radius: 50%; +} diff --git a/mandatory/3-slideshow/index.html b/mandatory/3-slideshow/index.html index 39cd40e..6890984 100644 --- a/mandatory/3-slideshow/index.html +++ b/mandatory/3-slideshow/index.html @@ -12,6 +12,20 @@ - + +
+ carousel image +
+
+ + + + + +
+
+ + +
diff --git a/mandatory/3-slideshow/slideshow.js b/mandatory/3-slideshow/slideshow.js index b55091c..d754f99 100644 --- a/mandatory/3-slideshow/slideshow.js +++ b/mandatory/3-slideshow/slideshow.js @@ -1 +1,81 @@ // Write your code here +let images = [ + "https://images.unsplash.com/photo-1592882542040-26ce2e12ee73?ixid=MXwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHw%3D&ixlib=rb-1.2.1&auto=format&fit=crop&w=1948&q=80", + "https://images.unsplash.com/photo-1607641513240-f43c6917515a?ixid=MXwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHw%3D&ixlib=rb-1.2.1&auto=format&fit=crop&w=736&q=80", + "https://images.unsplash.com/photo-1590249351139-c6effec78d2a?ixid=MXwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHw%3D&ixlib=rb-1.2.1&auto=format&fit=crop&w=1950&q=80", + "https://images.unsplash.com/photo-1607550295261-851fa60d8ed2?ixid=MXwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHw%3D&ixlib=rb-1.2.1&auto=format&fit=crop&w=1351&q=80", + "https://images.unsplash.com/photo-1607161744726-e96856cfcf4f?ixid=MXwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHw%3D&ixlib=rb-1.2.1&auto=format&fit=crop&w=1490&q=80" +]; + + + +document.addEventListener('DOMContentLoaded', function () { + document.querySelector("img").src = images[0]; + let counter = 0; + let myTimer; + let timeInterval = 5000; + let direction = "off"; + + function assignImage() { + counter = counter < 0 ? images.length - 1 : counter % images.length; + document.querySelector("img").src = images[counter]; + } + + let autoBack = document.querySelector("#autoBack") + autoBack.addEventListener("click", () => { + clearInterval(myTimer); + direction = "back"; + myTimer = setInterval(() => { + counter--; + assignImage(); + }, timeInterval); + }); + + let back = document.querySelector("#back") + back.addEventListener("click", () => { + clearInterval(myTimer); + counter--; + assignImage(); + }); + + let stop = document.querySelector("#stop") + stop.addEventListener("click", () => { + clearInterval(myTimer); + }); + + let forward = document.querySelector("#forward") + forward.addEventListener("click", () => { + clearInterval(myTimer); + counter++; + assignImage(); + }); + + let autoForward = document.querySelector("#autoForward") + autoForward.addEventListener("click", () => { + clearInterval(myTimer); + direction = "forward"; + myTimer = setInterval(() => { + counter++; + assignImage(); + }, timeInterval); + }); + + let changeTime = document.querySelector("#uiButton") + changeTime.addEventListener("click", () => { + timeInterval = document.querySelector("#ui").value * 1000; + clearInterval(myTimer); + if (direction === "back") { + myTimer = setInterval(() => { + counter--; + assignImage(); + }, timeInterval); + } else if (direction === "forward") { + myTimer = setInterval(() => { + counter++; + assignImage(); + }, timeInterval); + } + }); +}); + + diff --git a/mandatory/3-slideshow/style.css b/mandatory/3-slideshow/style.css index 63cedf2..626e859 100644 --- a/mandatory/3-slideshow/style.css +++ b/mandatory/3-slideshow/style.css @@ -1 +1,44 @@ /** Write your CSS in here **/ +img { + max-width: 100%; + max-height: 100%; + display: block; + margin-left: auto; + margin-right: auto; + padding: 1rem; +} + +.imgDiv { + height: 400px; + padding: 2rem; +} + +#uiButton { + max-width: 300px; +} + +.buttons { + display: flex; + padding: 1rem; + justify-content: center; + align-items: center; +} + +button { + margin-left: 1rem; +} + +body { + background-color: #ffffcc; + max-width: 700px; + padding: 2rem; + border-radius: 25px; + margin: 3rem; +} + +html { + display: flex; + flex-direction: column; + align-items: center; + background-color: wheat; +}