What is the problem with this code OCR computer Science

Watch
Man_Like_$
Badges: 12
Rep:
?
#1
Report Thread starter 4 years ago
#1
<!DOCTYPE html>
<html>
<head>
<title> Task 3 </>
</head>

<button type = "button" onclick="Switch()">Change Lights</button>
<img id="img" src="red.jpg"

<script>
var images = [], x=-1:
images[0] = "red.png"
images[1] = "yellow.png"
images[2] = "green.png"


function Switch() {
x = (x===images.length - 1 ) ? 0 : x+1
document.getElementById ("img").src = images[x]

</script>
</body>
</html>
0
reply
amrit_sv
Badges: 7
Rep:
?
#2
Report 4 years ago
#2
(Original post by Man_Like_$)
<!DOCTYPE html>
<html>
<head>
<title> Task 3 </>
</head>

<button type = "button" onclick="Switch()">Change Lights</button>
<img id="img" src="red.jpg"

<script>
var images = [], x=-1:
images[0] = "red.png"
images[1] = "yellow.png"
images[2] = "green.png"


function Switch() {
x = (x===images.length - 1 ) ? 0 : x+1
document.getElementById ("img").src = images[x]

</script>
</body>
</html>
Hi, you had a number of syntax errors which I have corrected for you below. Im assuming the path to your images is correct otherwise it still will not work so check that please.

You have also changed the file extension to .png when you add the images to the array so i changed them back to jpg but again just make sure the file path and file name are correct.

I assumed you wanted the switch function to loop through red, yellow and green so I have corrected that logic for you. Let me know if you still need any help.

Code:
<!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>
0
reply
Man_Like_$
Badges: 12
Rep:
?
#3
Report Thread starter 4 years ago
#3
(Original post by amrit_sv)
Hi, you had a number of syntax errors which I have corrected for you below. Im assuming the path to your images is correct otherwise it still will not work so check that please.

You have also changed the file extension to .png when you add the images to the array so i changed them back to jpg but again just make sure the file path and file name are correct.

I assumed you wanted the switch function to loop through red, yellow and green so I have corrected that logic for you. Let me know if you still need any help.
Code:
<!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>
Thanks it now works
0
reply
amrit_sv
Badges: 7
Rep:
?
#4
Report 4 years ago
#4
(Original post by Man_Like_$)
Thanks it now works
Just some pointers:

  • 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:

Code:
if ((x + 1) > images.length) {
    x = 0;
} else {
    x += 1;
}
0
reply
X

Quick Reply

Attached files
Write a reply...
Reply
new posts
Back
to top
Latest
My Feed

See more of what you like on
The Student Room

You can personalise what you see on TSR. Tell us a little about yourself to get started.

Personalise

Do you think receiving Teacher Assessed Grades will impact your future?

I'm worried it will negatively impact me getting into university/college (218)
44.13%
I'm worried that I'm not academically prepared for the next stage in my educational journey (56)
11.34%
I'm worried it will impact my future career (36)
7.29%
I'm worried that my grades will be seen as 'lesser' because I didn't take exams (104)
21.05%
I don't think that receiving these grades will impact my future (53)
10.73%
I think that receiving these grades will affect me in another way (let us know in the discussion!) (27)
5.47%

Watched Threads

View All