Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Javascript challenge...

Options
  • 26-03-2007 11:59pm
    #1
    Registered Users 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 Posts: 7,468 ✭✭✭Evil Phil


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


  • Registered Users Posts: 6,509 ✭✭✭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