Turn on thread page Beta
    • Thread Starter
    Offline

    0
    ReputationRep:
    We have started to learn Java and have been set the task to make and array, however my code outputs a blank web page.
    I looked online and emailed my teacher asking for help and they all indicated to my code being correct.

    This is my code:
    <script>
    <html>
    //<head>
    //<title>JavaScript Array Sort</title>
    //</head>
    <body>
    <script type="text/javascript">
    var products = ["Printer","Tablet","Router","Key board","Screen","","Mouse"];

    products.sort(function(a, b){return a-b});
    //console.log('Returned string is : ' + products );
    </script>

    </body>
    </html>
    •  Official Rep
    Offline

    15
    ReputationRep:
     Official Rep
    Sorry you've not had any responses about this. Are you sure you've posted in the right place? Here's a link to our subject forum which should help get you more responses if you post there.


    Just quoting in Amusing Elk so she can move the thread if needed :wizard:
    Spoiler:
    Show
    (Original post by Amusing Elk)
    x
    Offline

    12
    ReputationRep:
    (Original post by Matthew_J)
    We have started to learn Java and have been set the task to make and array, however my code outputs a blank web page.
    I looked online and emailed my teacher asking for help and they all indicated to my code being correct.

    This is my code:
    <script>
    <html>
    //<head>
    //<title>JavaScript Array Sort</title>
    //</head>
    <body>
    <script type="text/javascript">
    var products = ["Printer","Tablet","Router","Key board","Screen","","Mouse"];

    products.sort(function(a, b){return a-b});
    //console.log('Returned string is : ' + products );
    </script>

    </body>
    </html>
    Your actual logic is correct, however here are some issues with your code:

    -Your script tag is declared before your html tag
    -Obviously console.log will not display output to the screen, just to the console (I don't know if you knew that)

    Here is the fixed and working code:

    <html>
    <head>
    <title>JavaScript Array Sort</title>
    </head>
    <body>
    <script type="text/javascript">
    var products = ["Printer", "Tablet", "Router", "Key board", "Screen", "", "Mouse"];
    products.sort(function(a, b) { return a - b });
    document.write('Returneding is : ' + products);
    </script>
    </body>
    </html>

    Very simple errors that are easy to miss. Also please refer to Javascript as Javascript, and not Java. I'm sure you know that they're two different languages .
 
 
 
Reply
Submit reply
Turn on thread page Beta
Updated: February 12, 2017
Poll
Do you think parents should charge rent?

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

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