Skip to content

Commit d780bd2

Browse files
committed
sprint 3 sorted
1 parent 585ed73 commit d780bd2

File tree

10 files changed

+241
-14
lines changed

10 files changed

+241
-14
lines changed

Sprint-3/alarmclock/alarmclock.js

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,46 @@
1-
function setAlarm() {}
1+
let timer;
2+
let secondsRemaining = 0;
3+
4+
function setAlarm() {
5+
// Clear existing timer if any
6+
if (timer) {
7+
clearInterval(timer);
8+
}
9+
10+
// Get the time from the input field
11+
const alarmInput = document.getElementById("alarmSet");
12+
secondsRemaining = parseInt(alarmInput.value, 10);
13+
14+
// Update the display immediately
15+
updateTimeDisplay();
16+
17+
// Start the timer
18+
timer = setInterval(function() {
19+
secondsRemaining--;
20+
21+
// Update the display
22+
updateTimeDisplay();
23+
24+
// Check if time is up
25+
if (secondsRemaining <= 0) {
26+
clearInterval(timer);
27+
playAlarm();
28+
}
29+
}, 1000);
30+
}
31+
32+
function updateTimeDisplay() {
33+
const minutes = Math.floor(secondsRemaining / 60);
34+
const seconds = secondsRemaining % 60;
35+
36+
// Format with leading zeros
37+
const formattedMinutes = String(minutes).padStart(2, '0');
38+
const formattedSeconds = String(seconds).padStart(2, '0');
39+
40+
// Update the display
41+
document.getElementById("timeRemaining").innerText =
42+
`Time Remaining: ${formattedMinutes}:${formattedSeconds}`;
43+
}
244

345
// DO NOT EDIT BELOW HERE
446

Sprint-3/alarmclock/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<meta charset="utf-8" />
55
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
66
<link rel="stylesheet" href="style.css" />
7-
<title>Title here</title>
7+
<title>Alarm Clock</title>
88
</head>
99
<body>
1010
<div class="centre">

Sprint-3/quote-generator/index.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
<head>
44
<meta charset="UTF-8" />
55
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
6-
<title>Title here</title>
6+
<title>Quote Generator</title>
77
<script defer src="quotes.js"></script>
88
</head>
99
<body>
10-
<h1>hello there</h1>
10+
<h1>Random Quote Generator</h1>
1111
<p id="quote"></p>
1212
<p id="author"></p>
1313
<button type="button" id="new-quote">New quote</button>

Sprint-3/quote-generator/quotes.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -491,3 +491,23 @@ const quotes = [
491491
];
492492

493493
// call pickFromArray with the quotes array to check you get a random quote
494+
495+
// Function to display a random quote
496+
function displayRandomQuote() {
497+
// Get a random quote object from the quotes array
498+
const randomQuoteObject = pickFromArray(quotes);
499+
500+
// Get the quote and author elements
501+
const quoteElement = document.getElementById("quote");
502+
const authorElement = document.getElementById("author");
503+
504+
// Set the content of the elements
505+
quoteElement.innerText = randomQuoteObject.quote;
506+
authorElement.innerText = randomQuoteObject.author;
507+
}
508+
509+
// Display a quote when the page loads
510+
window.addEventListener("load", displayRandomQuote);
511+
512+
// Add event listener to the "New quote" button
513+
document.getElementById("new-quote").addEventListener("click", displayRandomQuote);

Sprint-3/reading-list/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<meta charset="utf-8" />
55
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
66
<link rel="stylesheet" href="style.css" />
7-
<title>Title here</title>
7+
<title>Reading List</title>
88
</head>
99
<body>
1010
<div id="content">

Sprint-3/reading-list/script.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,35 @@ const books = [
2121
},
2222
];
2323

24+
// Function to display the reading list
25+
function displayReadingList() {
26+
const readingList = document.getElementById("reading-list");
27+
28+
// Loop through each book and create list items
29+
books.forEach(book => {
30+
// Create list item
31+
const li = document.createElement("li");
32+
33+
// Set background color based on whether the book has been read
34+
li.style.backgroundColor = book.alreadyRead ? "green" : "red";
35+
36+
// Create and add the book cover image
37+
const img = document.createElement("img");
38+
img.src = book.bookCoverImage;
39+
li.appendChild(img);
40+
41+
// Create and add the book information
42+
const bookInfo = document.createElement("div");
43+
bookInfo.innerHTML = `
44+
<h2>${book.title}</h2>
45+
<p>by ${book.author}</p>
46+
`;
47+
li.appendChild(bookInfo);
48+
49+
// Add the list item to the reading list
50+
readingList.appendChild(li);
51+
});
52+
}
53+
54+
// Call the function when the page loads
55+
window.addEventListener("load", displayReadingList);

Sprint-3/slideshow/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<head>
44
<meta charset="UTF-8" />
55
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
6-
<title>Title here</title>
6+
<title>Image carousel</title>
77
<script defer src="slideshow.js"></script>
88
</head>
99
<body>

Sprint-3/slideshow/slideshow.js

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,63 @@ const images = [
44
"./assets/cute-cat-c.jpg",
55
];
66

7+
const imgElement = document.getElementById("carousel-img");
8+
const forwardBtn = document.getElementById("forward-btn");
9+
const backwardBtn = document.getElementById("backward-btn");
710

8-
// Write your code here
11+
let currentImageIndex = 0;
12+
13+
function moveForward() {
14+
currentImageIndex = (currentImageIndex + 1) % images.length;
15+
imgElement.src = images[currentImageIndex];
16+
}
17+
18+
function moveBackward() {
19+
currentImageIndex = (currentImageIndex - 1 + images.length) % images.length;
20+
imgElement.src = images[currentImageIndex];
21+
}
22+
23+
forwardBtn.addEventListener("click", moveForward);
24+
backwardBtn.addEventListener("click", moveBackward);
25+
26+
let autoInterval = null;
27+
const AUTO_INTERVAL_TIME = 2000; // 2 seconds
28+
29+
const autoForwardBtn = document.createElement("button");
30+
autoForwardBtn.id = "auto-forward";
31+
autoForwardBtn.textContent = "Auto Forward";
32+
document.body.appendChild(autoForwardBtn);
33+
34+
const autoBackwardBtn = document.createElement("button");
35+
autoBackwardBtn.id = "auto-backward";
36+
autoBackwardBtn.textContent = "Auto Backward";
37+
document.body.appendChild(autoBackwardBtn);
38+
39+
const stopBtn = document.createElement("button");
40+
stopBtn.id = "stop";
41+
stopBtn.textContent = "Stop";
42+
document.body.appendChild(stopBtn);
43+
44+
function startAutoForward() {
45+
clearInterval(autoInterval);
46+
autoInterval = setInterval(moveForward, AUTO_INTERVAL_TIME);
47+
autoForwardBtn.disabled = true;
48+
autoBackwardBtn.disabled = true;
49+
}
50+
51+
function startAutoBackward() {
52+
clearInterval(autoInterval);
53+
autoInterval = setInterval(moveBackward, AUTO_INTERVAL_TIME);
54+
autoForwardBtn.disabled = true;
55+
autoBackwardBtn.disabled = true;
56+
}
57+
58+
function stopAuto() {
59+
clearInterval(autoInterval);
60+
autoInterval = null;
61+
autoForwardBtn.disabled = false;
62+
autoBackwardBtn.disabled = false;
63+
}
64+
autoForwardBtn.addEventListener("click", startAutoForward);
65+
autoBackwardBtn.addEventListener("click", startAutoBackward);
66+
stopBtn.addEventListener("click", stopAuto);

Sprint-3/todo-list/index.html

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,17 @@
33
<head>
44
<meta charset="UTF-8" />
55
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
6-
<title>Title here</title>
6+
<title>Todo List</title>
77
<link rel="stylesheet" href="style.css" />
8+
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css" />
89
</head>
910
<body>
1011
<form>
1112
<div>
12-
<input type="text" placeholder="New todo..." />
13+
<input type="text" id="todoInput" placeholder="New todo..." />
1314
</div>
1415
<div>
15-
<button type="submit">Add Todo</button>
16+
<button type="submit" class="btn">Add Todo</button>
1617
<button
1718
type="button"
1819
id="remove-all-completed"
@@ -22,6 +23,11 @@
2223
</button>
2324
</div>
2425
</form>
26+
27+
<ul id="todo-list">
28+
<!-- Todo items will be added here by JavaScript -->
29+
</ul>
30+
2531
<script src="script.js"></script>
2632
</body>
2733
</html>

Sprint-3/todo-list/script.js

Lines changed: 73 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,50 @@
11
function populateTodoList(todos) {
22
let list = document.getElementById("todo-list");
3-
// Write your code to create todo list elements with completed and delete buttons here, all todos should display inside the "todo-list" element.
3+
4+
// Clear existing todos
5+
list.innerHTML = "";
6+
7+
// Add each todo to the list
8+
todos.forEach(todo => {
9+
const li = document.createElement("li");
10+
li.textContent = todo.task;
11+
12+
// Apply line-through style if completed
13+
if (todo.completed) {
14+
li.style.textDecoration = "line-through";
15+
}
16+
17+
// Create tick icon
18+
const tickIcon = document.createElement("i");
19+
tickIcon.className = "fas fa-check";
20+
tickIcon.addEventListener("click", function() {
21+
// Toggle line-through style
22+
if (li.style.textDecoration === "line-through") {
23+
li.style.textDecoration = "none";
24+
todo.completed = false;
25+
} else {
26+
li.style.textDecoration = "line-through";
27+
todo.completed = true;
28+
}
29+
});
30+
31+
// Create bin/trash icon
32+
const binIcon = document.createElement("i");
33+
binIcon.className = "fas fa-trash";
34+
binIcon.addEventListener("click", function() {
35+
// Remove the todo item from the list
36+
li.remove();
37+
// Remove from the todos array
38+
todos = todos.filter(t => t !== todo);
39+
});
40+
41+
// Append icons to the list item
42+
li.appendChild(tickIcon);
43+
li.appendChild(binIcon);
44+
45+
// Append the list item to the list
46+
list.appendChild(li);
47+
});
448
}
549

650
// These are the same todos that currently display in the HTML
@@ -16,10 +60,35 @@ populateTodoList(todos);
1660
function addNewTodo(event) {
1761
// The code below prevents the page from refreshing when we click the 'Add Todo' button.
1862
event.preventDefault();
19-
// Write your code here... and remember to reset the input field to be blank after creating a todo!
63+
64+
// Get input value
65+
const inputField = document.getElementById("todoInput");
66+
const taskValue = inputField.value.trim();
67+
68+
if (taskValue) {
69+
// Add new todo to the array
70+
const newTodo = { task: taskValue, completed: false };
71+
todos.push(newTodo);
72+
73+
// Update the list
74+
populateTodoList(todos);
75+
76+
// Clear the input field
77+
inputField.value = "";
78+
}
2079
}
2180

22-
// Advanced challenge: Write a fucntion that checks the todos in the todo list and deletes the completed ones (we can check which ones are completed by seeing if they have the line-through styling applied or not).
81+
// Add event listener for the form submission
82+
document.querySelector("form").addEventListener("submit", addNewTodo);
83+
84+
// Advanced challenge: Write a function that checks the todos in the todo list and deletes the completed ones (we can check which ones are completed by seeing if they have the line-through styling applied or not).
2385
function deleteAllCompletedTodos() {
24-
// Write your code here...
86+
// Filter out completed todos
87+
todos = todos.filter(todo => !todo.completed);
88+
89+
// Update the list
90+
populateTodoList(todos);
2591
}
92+
93+
// Add event listener for the remove all completed button
94+
document.getElementById("remove-all-completed").addEventListener("click", deleteAllCompletedTodos);

0 commit comments

Comments
 (0)