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
Read the string of “n” characters into a variable
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
Read the string into a variable. This will be referred-to later as the “original incoming string”
Read the “look-for” string into another variable.
Start a counter at 0
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
Get the last digit of the time
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.
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.