Join TSR now and get all your revision questions answeredSign up now

What is the problem with this code OCR computer Science Watch

    • Thread Starter
    Offline

    1
    ReputationRep:
    <!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>
    Online

    2
    ReputationRep:
    (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>
    • Thread Starter
    Offline

    1
    ReputationRep:
    (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
    Online

    2
    ReputationRep:
    (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;
    }
 
 
 
Poll
Is GoT overrated?

The Student Room, Get Revising and Marked by Teachers are trading names of The Student Room Group Ltd.

Register Number: 04666380 (England and Wales), VAT No. 806 8067 22 Registered Office: International House, Queens Road, Brighton, BN1 3XE

Quick reply
Reputation gems: You get these gems as you gain rep from other members for making good contributions and giving helpful advice.