www.webdeveloper.com
Results 1 to 2 of 2

Thread: simple javascript question

  1. #1
    Join Date
    Nov 2010
    Posts
    1

    simple javascript question

    Hi, I'm trying to work on basic javascript problem.

    But, I have no idea for this, because I still didn't catch up what function should I used and where and when.

    So, here is problem and script is below

    Let the user type a word. Display the word in color, such that each letter's position in the alphaabet determines its color. Try using a string containing the alphabet and indexOf to find the position of each input letter.

    ** I have no idea what the heck is the indexOf function and how convert it thru word. So, please please solve this "simple" problem for you guys. Thank you.
    Code:
    <html>
    <head>
    <script language="JavaScript">
    <!-- hide
    
       colors    = new Array; // "
      colors[0]="red";
      colors[1]="blue";
      colors[2]="green";
      colors[3]="purple";
      colors[4]="orange";
      colors[5]="lime";
      colors[6]="cyan";
    
    
    var first=window.prompt("Please enter the "Hello world!"","");
    var str="Hello world!";
    var a = 'Hello world!';
    document.write(a+'')
    var i=0;
    var count=0;
    var b="d";
    document.writeln("<font size=+3>" + "<br>");
    for(i=0;i<str.length;i++)
      {
    
    
    	if (str.charAt(i)==b)
    		count++;
    	
      }
    
    
    document.write(count );
    document.writeln("<hr>");
    document.writeln("<br>");
    //-->
    </script>
    </head>
    <body>
    <font size=+2>
    The preceding text was generated entirely by JavaScript code in this page.
    <br>
    </font>
    </body>
    </html>

  2. #2
    Join Date
    Mar 2007
    Location
    U.K.
    Posts
    1,127
    Quote Originally Posted by gorush View Post
    ** I have no idea what the heck is the indexOf function and how convert it thru word.
    If this is a homework question then you should have been taught that, or at least be able to look it up in your textbook. You have heard of Google?

    In any case the alphabet string / indexOf idea is a poor solution.

    Look up the string functions charAt(), charCodeAt(), toLowerCase().
    https://developer.mozilla.org/en/Jav...#String_Object

    Remember that arrays have a .length property.

    Check out the % (remainder / modulus) operator: https://developer.mozilla.org/en/Jav...etic_Operators

    You do know how to use prompt() ?

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles