Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Javascript challenge...

  • 26-03-2007 11:59PM
    #1
    Registered Users, Registered Users 2 Posts: 1,674 ✭✭✭


    Ok below is a javascript code which writes a random font and font size on the text it is given, every time it is refreshed, it works, but it has a problem which you will see if you run it. The challenge is to get it working.
    Can you find the error?
    Good luck.
    <html>
    <head>
    <script>
    var fontList = new Array ("serif","sans serif","monospace","cursive");
    var sizeList = new Array("1","2","3","4","5","6","7");

    function randomNumber( max) {
    return Math.floor(Math.random()*(max +1));
    }

    function randomFont( str ) {
    var strArr = new Array();
    for (i = 0; i<str.length; i++) {
    strArr = str.substr(i,i+1);
    }

    var newstr = "";

    for (i=0; i<strArr.length; i++) {
    newstr += "<font face=\"" + fontList[randomNumber(fontList.length)] + "\" size=\"" + sizeList[randomNumber(sizeList.length)] + "\">" + strArr + "</font>";
    }
    return newstr;
    }
    </script>
    </head>

    <body>

    <script language="JavaScript" type="text/javascript">
    <!--
    document.write( randomFont("This is the string") );
    document.write("<br>");
    document.write( randomFont("which is being passed"));
    //-->
    </script>

    </p>
    <font size="7">Test</font>
    </body>
    </html>
    Just a bit of fun for programmers.


Comments

  • Registered Users, Registered Users 2 Posts: 7,468 ✭✭✭Evil Phil


    Yes, I can find the error. The question is: is this your homework? :)


  • Registered Users, Registered Users 2 Posts: 6,652 ✭✭✭daymobrew


    Is the error that it is not using CSS? :p


  • Users Awaiting Email Confirmation Posts: 351 ✭✭ron_darrell


    1. replace substr with substring
    2. change max + 1 to max;
    3. name actual fonts not general families


Advertisement