Skip to content

Commit 15fb0cb

Browse files
Add files via upload
1 parent d8765de commit 15fb0cb

31 files changed

+1384
-0
lines changed

alert.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
alert("Hello! Welcome to JavaScript learning.");

bulb.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
function toggleLight() {
2+
let lightbulb = document.getElementById("lightbulb");
3+
let body = document.body;
4+
if (lightbulb.src.includes("lightbulb_off.png")) {
5+
lightbulb.src = "media/lightbulb_on.png";
6+
body.classList.add("dark-mode");
7+
} else {
8+
lightbulb.src = "media/lightbulb_off.png";
9+
body.classList.remove("dark-mode");
10+
}
11+
}

calculator.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
function addab(a, b) {
2+
return a + b;
3+
}
4+
const subtract = function (a, b) {
5+
return a - b;
6+
};
7+
const multiply = (a, b) => a * b;
8+
const divide = (a, b) =>
9+
b !== 0 ? (a / b).toFixed(2) : "Cannot divide by zero";
10+
11+
function calculate() {
12+
let num2Input = document.getElementById("num2").value;
13+
let num3Input = document.getElementById("num3").value;
14+
let operation = document.getElementById("operation").value;
15+
let result;
16+
17+
// Input validation
18+
if (num2Input === "" || num3Input === "") {
19+
document.getElementById("output7").innerHTML = "Please enter both numbers.";
20+
return;
21+
}
22+
23+
let num2 = parseFloat(num2Input);
24+
let num3 = parseFloat(num3Input);
25+
26+
if (isNaN(num2) || isNaN(num3)) {
27+
document.getElementById("output7").innerHTML =
28+
"Please enter valid numbers.";
29+
return;
30+
}
31+
32+
switch (operation) {
33+
case "add":
34+
result = addab(num2, num3);
35+
break;
36+
case "subtract":
37+
result = subtract(num2, num3);
38+
break;
39+
case "multiply":
40+
result = multiply(num2, num3);
41+
break;
42+
case "divide":
43+
result = divide(num2, num3);
44+
break;
45+
default:
46+
result = "Invalid Operation";
47+
}
48+
49+
document.getElementById(
50+
"output7"
51+
).innerHTML = `Result: <strong>${result}</strong>`;
52+
}

clickevent.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
document.getElementById("colorButton").addEventListener("click", function () {
2+
const colors = ["red", "blue", "black"];
3+
let currentColor = this.style.backgroundColor;
4+
let nextColor = colors[(colors.indexOf(currentColor) + 1) % colors.length];
5+
this.style.backgroundColor = nextColor;
6+
});

clock.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
let clockInterval;
2+
let isRunning = false;
3+
4+
function updateClock() {
5+
let now = new Date();
6+
let hours = String(now.getHours()).padStart(2, "0");
7+
let minutes = String(now.getMinutes()).padStart(2, "0");
8+
let seconds = String(now.getSeconds()).padStart(2, "0");
9+
document.getElementById("clock").innerHTML = `${hours}:${minutes}:${seconds}`;
10+
}
11+
12+
document.getElementById("toggleClock").addEventListener("click", function () {
13+
if (!isRunning) {
14+
clockInterval = setInterval(updateClock, 1000);
15+
this.textContent = "Stop Clock";
16+
} else {
17+
clearInterval(clockInterval);
18+
this.textContent = "Start Clock";
19+
}
20+
isRunning = !isRunning;
21+
});

colormixer.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
function updateColor() {
2+
let red = document.getElementById("red").value;
3+
let green = document.getElementById("green").value;
4+
let blue = document.getElementById("blue").value;
5+
6+
document.getElementById("redValue").innerText = red;
7+
document.getElementById("greenValue").innerText = green;
8+
document.getElementById("blueValue").innerText = blue;
9+
10+
let rgbColor = `rgb(${red}, ${green}, ${blue})`;
11+
12+
const changeColor = (color) => {
13+
document.getElementById("colorBox").style.backgroundColor = color;
14+
};
15+
changeColor(rgbColor);
16+
}

counter.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
let count = 0;
2+
const counterDisplay = document.getElementById("counterDisplay");
3+
4+
document.getElementById("incrementBtn").addEventListener("click", function () {
5+
count++;
6+
counterDisplay.innerText = count;
7+
});
8+
9+
document.getElementById("decrementBtn").addEventListener("click", function () {
10+
count--;
11+
counterDisplay.innerText = count;
12+
});
13+
14+
document.getElementById("resetBtn").addEventListener("click", function () {
15+
count = 0;
16+
counterDisplay.innerText = count;
17+
});

darkmode.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
document
2+
.getElementById("toggle-dark-mode")
3+
.addEventListener("click", function () {
4+
const toggleButton = document.getElementById("toggle-dark-mode");
5+
document.body.classList.toggle("dark-mode");
6+
toggleButton.src = document.body.classList.contains("dark-mode")
7+
? "media/light_mode.png"
8+
: "media/dark_mode.png";
9+
});

domm.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
function changeHeading() {
2+
const options = [
3+
"The only way to do great work is to love what you do. - Steve Jobs",
4+
"Strive not to be a success, but rather to be of value. - Albert Einstein",
5+
"The future belongs to those who believe in the beauty of their dreams. - Eleanor Roosevelt",
6+
"It does not matter how slowly you go as long as you do not stop. - Confucius",
7+
"Our greatest glory is not in never failing, but in rising up every time we fail. - Ralph Waldo Emerson",
8+
"The journey of a thousand miles begins with a single step. - Lao Tzu",
9+
"Believe you can and you're halfway there. - Theodore Roosevelt",
10+
"Success is not final, failure is not fatal: it is the courage to continue that counts. - Winston S. Churchill",
11+
"What you get by achieving your goals is not as important as what you become by achieving your goals. - Zig Ziglar",
12+
"The best way to predict the future is to create it. - Peter Drucker",
13+
];
14+
const randomIndex = Math.floor(Math.random() * options.length);
15+
document.getElementById("domHeading").innerText = options[randomIndex];
16+
}
17+
18+
function addNewParagraph() {
19+
const p = document.createElement("p");
20+
p.innerText = "This is a new paragraph!";
21+
document.getElementById("domContent").appendChild(p);
22+
}
23+
document.addEventListener("DOMContentLoaded", function () {
24+
const darkModeButton = document.getElementById("darkmodetoggle");
25+
const toggleDarkModeImage = document.getElementById("toggle-dark-mode");
26+
darkModeButton.addEventListener("click", function () {
27+
toggleDarkModeImage.click();
28+
});
29+
});

domma.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
function changeColour() {
2+
let colours = [
3+
"lightsalmon",
4+
"lightseagreen",
5+
"lightskyblue",
6+
"lightsteelblue",
7+
"lightslategray",
8+
"lightcyan",
9+
"lightcoral",
10+
"lightgoldenrodyellow",
11+
"lightgray",
12+
"lightgreen",
13+
"lightpink",
14+
"lightslategray",
15+
"lightyellow",
16+
];
17+
let randomColour = colours[Math.floor(Math.random() * colours.length)];
18+
document.getElementById("colourBox").style.backgroundColor = randomColour;
19+
}
20+
21+
function addItems() {
22+
let input = document.getElementById("itemInputs").value;
23+
if (input === "") {
24+
alert("Please enter a task!");
25+
return;
26+
}
27+
28+
let li = document.createElement("li");
29+
li.innerHTML = `${input} <button onclick="removeItem(this)">⛔</button>`;
30+
document.getElementById("taskLists").appendChild(li);
31+
32+
document.getElementById("itemInputs").value = "";
33+
}
34+
35+
function removeItem(element) {
36+
element.parentElement.remove();
37+
}

0 commit comments

Comments
 (0)