Please help for the javascript

I am trying to work on for a script. But, it doesn’t work.

Here is the problem.

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.

And here is the hint.
Processing the characters of user-input, one character at a time

  1. Read the string of “n” characters into a variable
  2. Build a loop, then inside it:
    a. Extract the nth character of the input as subscripted by your loop.
    b. Use indexof with that string and a string containing the whole alphabet.
    c. Now you have the position (in the alphabet) of the input character as a number. Use that number as in index into a color array of 26 colors.
    d. Use document.write to write that character to the screen in the indexed color.
    e. Repeat the loop until you have extracted the last character
    Counting characters in a string
  3. Read the string into a variable. This will be referred-to later as the “original incoming string”
  4. Read the “look-for” string into another variable.
  5. Start a counter at 0
  6. Build a loop and inside it:
    a. Use indexOf with the 2 variables, to find the look-for string and SAVE ITS POSITION into a THIRD variable and add 1 to a counter
    b. Use substring or substr to assign the PART of the incoming string that starts at 1+ the position you just found back into the “original incoming-string” variable (thereby replacing the original). You now have NEW “original”
    c. Let the loop repeat as long as the length of the “original” >0
  7. Get the last digit of the time
  8. Use that digit as an index into an array of colors.

This should be using document.write and string script only. Please please write a script as a beginner user’s view. Thank you.

There are many here who believe that doing someone elses homework is highly unethical.

is this the same question as in this thread?

using document.write() is not, imho, the correct way to do this. You should be using DOM methods.

but if you post the code you have so far we can try to help you debug it.

in the mean time this is the way I would do it.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
<html xmlns="">
<script type="text/javascript">
var colorsA = ['antiquewhite','aqua','blue','blueviolet','brown','cadetblue','chartreuse','chocolate','coral','crimson','darkcyan','darkgreen','darkkhaki','darkred','darkslateblue','deeppink','dimgray','firebrick','goldenrod','hotpink','khaki','lightcoral','red','maroon','rosybrown','yellowgreen'];
var alphabetA = 'abcdefghijklmnopqrstuvwxyz';
window.onload=function() {
    var resultO = document.getElementById('result');
    var str = window.prompt('Enter a word','Hello');
    var chr,txtNode,pos,elem;
    for(var i=0; i < str.length; i++) {
        chr = str.charAt(i);
        pos = alphabetA.indexOf(chr.toLowerCase());
        elem = document.createElement('span'); = (pos != -1)? colorsA[pos] : '';
        txtNode = document.createTextNode(chr);
<div id="result"></div>



<script type=“text/javascript”>

<!-- hide

function setalphabet()


alphabet[0] = “a”;

alphabet[1] = “b”;

alphabet[2] = “c”;

alphabet[3] = “d”;

alphabet[4] = “e”;

alphabet[5] = “f”;

alphabet[6] = “g”;
alphabet[7] = “h”;
alphabet[8] = “i”;
alphabet[9] = “j”;
alphabet[10] = “k”;
alphabet[11] = “l”;
alphabet[12] = “m”;
alphabet[13] = “n”;
alphabet[14] = “o”;
alphabet[15] = “p”;
alphabet[16] = “q”;
alphabet[17] = “r”;
alphabet[18] = “s”;
alphabet[19] = “t”;
alphabet[20] = “u”;
alphabet[21] = “v”;
alphabet[22] = “w”;
alphabet[23] = “x”;
alphabet[24] = “y”;
alphabet[25] = “z”;


function setcolors()











alphabet = new Array; // define an array name

colors = new Array; // "



var first = window.prompt(“Type a word! Any word!”,“”);

document.write("You just typed the word: ".fontcolor(“colors[i]”)

  • first.fontcolor(“blue”) +

"; look at the colors

var i=0;

var count=0;

document.writeln(“<font size=+3>” + “<br>”);









<font size=+2>

The preceding text was generated entirely by JavaScript code in this page.





the very first error that comes up when I run your code is on this line

"; look at the colors

which is a part of this overall line

document.write("You just typed the word: ".fontcolor("colors[i]")

+ first.fontcolor("blue") +

"; look at the colors

that should be a very quck and easy fix for you.

when you fix that, see if any other errors come up.

I’m trying to engage with indexOf() and array function.

As you know, at this script, I already setup the array for color and alphabet.

but I have no clue where should I remove and fix it.

I really need to know how to engage indexOf() and array() function.

Such as, if the user typed “hello world”, and how should set the position for each alphabet using indexOf and array That’s what I need to know.

the code I posted shows you the logic and how to use indexOf().

I’ve essentially done your assignment. the only difference is that I used DOM methods for the output instead of document.write() that you want.

You can use the code I posted and simply take out the DOM methods I used to output the coloured letters and replace them with document.write() code to output the coloured letters.

The way I did it is about 90% of what you asked for.

you should be able to do the remaining 10%.