What is the problem with this code OCR computer Science

Announcements Posted on
Four things that unis think matter more than league tables 08-12-2016
    • Thread Starter
    Offline

    0
    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>
    Offline

    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

    0
    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
    Offline

    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;
    }
 
 
 
Write a reply… Reply
Submit reply

Register

Thanks for posting! You just need to create an account in order to submit the post
  1. this can't be left blank
    that username has been taken, please choose another Forgotten your password?
  2. this can't be left blank
    this email is already registered. Forgotten your password?
  3. this can't be left blank

    6 characters or longer with both numbers and letters is safer

  4. this can't be left empty
    your full birthday is required
  1. Oops, you need to agree to our Ts&Cs to register
  2. Slide to join now Processing…

Updated: October 12, 2016
TSR Support Team

We have a brilliant team of more than 60 Support Team members looking after discussions on The Student Room, helping to make it a fun, safe and useful place to hang out.

Poll
Do you think you'll achieve your predicted A Level grades?
Useful resources

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.