<!DOCTYPE html>
<html>
<head>
<title>Task 3</title>
</head>
<body>
<button onclick="Switch()" >Change Lights</button> <img id="img" src="red.jpg">
<script>
var images = [], x=0;
images[0] = "red.jpg"
images[1] = "yellow.jpg"
images[2] = "green.jpg"
function Switch () {
x + 1 > images.length - 1 ? x = 0 : x += 1
console.log(x)
document.getElementById("img").src = images[x]
}
</script>
</body>
<!DOCTYPE html>
<html>
<head>
<title>Task 3</title>
</head>
<body>
<button onclick="Switch()" >Change Lights</button> <img id="img" src="red.jpg">
<script>
var images = [], x=0;
images[0] = "red.jpg"
images[1] = "yellow.jpg"
images[2] = "green.jpg"
function Switch () {
x + 1 > images.length - 1 ? x = 0 : x += 1
console.log(x)
document.getElementById("img".src = images[x]
}
</script>
</body>
•
Make sure to close all tags e.g <h1> </h1>
You can delete the console.log(x) line its just there to show you the index of the array.
When using ternary operator, the equivalent in an if else statement would be:
if ((x + 1) > images.length) {
x = 0;
} else {
x += 1;
}
Last reply 5 minutes ago
Why is the political left now censorious and authoritarian??Last reply 5 minutes ago
OCR A-level Religious Studies Paper 3 (Christianity) - 20th June 2024 [Exam Chat]Last reply 7 minutes ago
Official London School of Economics and Political Science 2024 Applicant ThreadLast reply 7 minutes ago
IBM Degree Apprenticeship 2024Last reply 8 minutes ago
Weidenfeld Hoffmann Trust (WHT) Scholarship Notification (2024-2025)Last reply 18 minutes ago
LSE politics and Economics 2023/2024Last reply 18 minutes ago
Is it really such a bad thing to have an undefined relationshipLast reply 1 hour ago
Is MathsFM French Alevels good for a CS degree at top uni (OX, UCL, Durham etc)Last reply 22 hours ago
OCR A-level Computer Science Paper 1 (H446/01) - 12th June 2023 [Exam Chat]Last reply 1 week ago
OCR A-level Computer Science Paper 2 (H446/02) - 19th June 2023 [Exam Chat]Last reply 3 weeks ago
AQA A-level Computer Science Paper 1 (7517 1A - 1E) - 12th June 2023 [Exam Chat]Last reply 4 weeks ago
Not receiving any response from University of Birmingham.Last reply 3 months ago
OCR GCSE Computer Science Paper 1 (J277/01) - 19th May 2023 [Exam Chat]Last reply 3 months ago
AQA GCSE Computer Science Paper 2 (8525/2) - 25th May 2023 [Exam Chat]Last reply 1 hour ago
Is MathsFM French Alevels good for a CS degree at top uni (OX, UCL, Durham etc)Last reply 22 hours ago
OCR A-level Computer Science Paper 1 (H446/01) - 12th June 2023 [Exam Chat]Last reply 1 week ago
OCR A-level Computer Science Paper 2 (H446/02) - 19th June 2023 [Exam Chat]Last reply 3 weeks ago
AQA A-level Computer Science Paper 1 (7517 1A - 1E) - 12th June 2023 [Exam Chat]Last reply 4 weeks ago
Not receiving any response from University of Birmingham.Last reply 3 months ago
OCR GCSE Computer Science Paper 1 (J277/01) - 19th May 2023 [Exam Chat]Last reply 3 months ago
AQA GCSE Computer Science Paper 2 (8525/2) - 25th May 2023 [Exam Chat]