Skip to content
Closed
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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
node_modules
.DS_Store
.vscode
**/.DS_Store
**/.DS_Store
.qodo
1 change: 1 addition & 0 deletions Sprint-3/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.qodo
24 changes: 24 additions & 0 deletions Sprint-3/Prep/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<script defer src="./script.js"></script>
<section>
<h3>Example character limit comment component</h3>
<label for="comment-input">
Please enter your comment in the text area below
</label>
<textarea
id="comment-input"
name="comment-input"
rows="5"
maxlength="200"
></textarea>
<p id="character-limit-info">You have 200 characters remaining</p>
</section>
</body>
</html>
39 changes: 39 additions & 0 deletions Sprint-3/Prep/script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Given an textarea and a character limit of 200
// When a user types characters into the textarea
// // Then the interface should update with how many characters they’ve got left.

//Steps
// 1. Set a character limit ot the text area
// 2. Access the textarea in the DOM
// 3. When an event occurs, we compare characterLimit with the current length of the text area,
//4. We must calculate the characters left
//.5 Update the text with the actual characters left


const characterLimit =200;
const textArea = document.querySelector('#comment-input');



//Keyup event is fired when a key is released
//React to this event!

//Run some code, whenever the keyup event takes place?
textArea.addEventListener('keyup', function updateCharacterLimit(event) {
const characterLeft = characterLimit - textArea.value.length;
const characterLimitInfo = document.querySelector('#character-limit-info');

characterLimitInfo.innerText = ` You have ${characterLeft} characters remaining `;
});

//We could also create the event function outside of the event listener
// function updateCharacterLimit(event) {
// const characterLeft = characterLimit - textArea.value.length;
// const charLimitInfo = document.querySelector('#character-limit-info');

// characterLimitInfo.innerText = ` You have ${characterLeft} characters remaining `;
// }

// textArea.addEventListener('keyup',updateCharacterLimit);

// Notice if we build the function outside of the event listener, we can call it directly from the event listener and we don't use parenthesis anymore
68 changes: 66 additions & 2 deletions Sprint-3/alarmclock/alarmclock.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,67 @@
function setAlarm() {}
function setAlarm() {
//Converting the time of now value to seconds from milliseconds
//Getting the time value from the input field, converting it into number, this will be in seconds
const alarmTime = Number(document.getElementById("alarmSet").value);

document.getElementById('timeRemaining').innerText="Time Remaining: "+timeFormatter(alarmTime);
let currentTime=0;

// DO NOT EDIT BELOW HERE
const checkAlarm=setInterval(() => {

//Calculating current time inside the interval
currentTime++;

//Calculating the difference between the alarm time and the current time
const diff = alarmTime - currentTime;

//Updating the time remaining text in the HTML element
document.getElementById('timeRemaining').innerText="Time Remaining: "+timeFormatter(diff);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like the same code as is on line 6.

Let's imagine we wanted to change the text it showed from "Time Remaining:" to "Time Left:" - we would need to make sure we remembered to change both lines 6 and 18.

Can you think of a way to make it so that we would only need to change one line of code if we wanted to make that change, rather than two?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reason is as soon as the user set alarm the value is added to the title, I can only keep line 18, but the title updates after 1 second interval.
Now removed line 6, which means the title is updated after 1 second, if that needs to be changed and you prefer the user to see the title as soon as they click set alarm button, please let me know so I changed it to what it was before.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree that you need to update the element both when the alarm is set, and when it changes. What I'm asking here is: How can you do that without repeating exactly the same code?




//If the difference is less than or equal to zero, stop the interval and play the alarm sound
if(diff <= 0){
clearInterval(checkAlarm);
playAlarm();


}
}, 1000);
}

//formatting remaining seconds in the format of MM:SS
function timeFormatter(seconds) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We normally name functions like verbs because they do things - so formatTime rather than timeFormatter. (Same comment applies to timePadder)

const remainingSeconds = seconds % 60;
const remainingMinutes = Math.floor(seconds / 60);
return timePadder(remainingMinutes, remainingSeconds);
}
//Formatting the time in MM:SS format
function timePadder(min , sec){
const minPadded = min.toString().padStart(2, '0');
const secPadded = sec.toString().padStart(2, '0');
return `${minPadded}:${secPadded}`;

}

let flashInterval; // To store the interval ID for flashing

function flashBackground() {
let isRed = false;

flashInterval = setInterval(() => {
// Toggle the background color between red and white
document.body.style.backgroundColor = isRed ? '#FDF7FA' : '#D67AB1';
isRed = !isRed;
}, 1000); // Flash every second
}


function stopFlashing() {
clearInterval(flashInterval); // Stop the flashing by clearing the interval
document.body.style.backgroundColor = 'white'; // Reset the background color
}

// DO NOT EDIT BELOW HERE
var audio = new Audio("alarmsound.mp3");

function setup() {
Expand All @@ -16,10 +76,14 @@ function setup() {

function playAlarm() {
audio.play();
//to flash background
flashBackground();
}

function pauseAlarm() {
audio.pause();
//to stop flashing background
stopFlashing();
}

window.onload = setup;
2 changes: 1 addition & 1 deletion Sprint-3/alarmclock/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" href="style.css" />
<title>Title here</title>
<title>Alarm Clock App</title>
</head>
<body>
<div class="centre">
Expand Down
Loading
Loading