<!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 1 minute ago
Official University of Edinburgh Offer Holders Thread for 2024 entryLast reply 2 minutes ago
Official London School of Economics and Political Science 2024 Applicant ThreadLast reply 7 minutes ago
Possibilities of making it into the Undergrad Economics?Last reply 11 minutes ago
2024 entry A100 / A101 Medicine fastest and slowest offer senders851
Last reply 12 minutes ago
JK Rowling in ‘arrest me’ challenge over hate crime lawLast reply 13 minutes ago
AQA A Level Sociology Paper 1 (7192/1) - 20th May 2024 [Exam Chat]Last reply 15 minutes ago
November 2023 Illegal Migration Intake Unit - Intake Response Team - Immigration OffiLast reply 21 minutes ago
Edexcel A Level Mathematics Paper 1 (9MA0 01) - 4th June 2024 [Exam Chat]Last reply 22 minutes ago
Meeting someone who reminds you exactly of someone you've met in the past1
Last reply 27 minutes ago
Can I do medicine with these gcse grades and what unis would be most likely to acceptLast reply 28 minutes ago
March 2024 Cambridge Mature Colleges Round - Anybody?Last reply 1 week ago
OCR A-level Computer Science Paper 2 (H446/02) - 19th June 2023 [Exam Chat]Last reply 2 weeks ago
AQA A-level Computer Science Paper 1 (7517 1A - 1E) - 12th June 2023 [Exam Chat]Last reply 3 weeks ago
OCR A-level Computer Science Paper 1 (H446/01) - 12th June 2023 [Exam Chat]Last reply 2 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 5 months ago
AQA GCSE Computer Science Paper 1 (8525/1A, 1B, 1C) - 19th May 2023 [Exam Chat]Last reply 5 months ago
AQA Computer Science NEA and Computer science paper 1 questionLast reply 1 week ago
OCR A-level Computer Science Paper 2 (H446/02) - 19th June 2023 [Exam Chat]Last reply 2 weeks ago
AQA A-level Computer Science Paper 1 (7517 1A - 1E) - 12th June 2023 [Exam Chat]Last reply 3 weeks ago
OCR A-level Computer Science Paper 1 (H446/01) - 12th June 2023 [Exam Chat]Last reply 2 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 5 months ago
AQA GCSE Computer Science Paper 1 (8525/1A, 1B, 1C) - 19th May 2023 [Exam Chat]Last reply 5 months ago
AQA Computer Science NEA and Computer science paper 1 question