Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-06-2012, 03:05 AM   PM User | #1
rhollu
New to the CF scene

 
Join Date: Nov 2012
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
rhollu is an unknown quantity at this point
[Urgent]Changing the font color with loop and array

stuck on a JavaScript homework...created the array but I can't use them in the code...what can i do to make the array usable?
Instruction:Your code will use an array to store the names of 5 colors: "green", "blue", "yellow", "orange", and "red". The code will prompt the user 4 times for a number between 0 and 4. If the user enters 0, the text color should change to "green", 1 to "blue", etc. If the user enters something that is not a number or not a number between 0 and 4, you should change the background to black.
here is what I got:
Quote:
<html>
<head>
<title>Changing Font Color</title>
</head>

<body bgcolor="white">
<h1 id="colors">
The color is black.
</h1>

<h1>
<script type="text/javascript">
// Comment row.

// This function will change the color of the text to the name of the color you give it.
for (i=0; i < 4; i++)
var colorText = window.prompt ("Enter a number from 0 to 4: ");
function changeColor(colorText)
{ var text = document.getElementById("colors[i]");
text.innerHTML = "<font color=\""+colorText+"\">The color is "+colorText+".</font>";
}


// Declare, create, and put values into your color array here
var colors = new Array (5);
colors[0] = "green";
colors[1] = "blue";
colors[2] = "yellow";
colors[3] = "orange";
colors[4] = "blue";
// Create a loop that runs 4 times.
// Each time it asks the user for a number between 0 and 4.
// If an invalid input is entered, the color displayed should be black.
// Otherwise display the color at that index in the array.
</script>
</h1>
</body>
</html>
rhollu is offline   Reply With Quote
Old 11-06-2012, 03:37 AM   PM User | #2
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,168
Thanks: 59
Thanked 3,993 Times in 3,962 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Makes no sense.

You have
Code:
<h1 id="colors">
but then you do
Code:
    document.getElementById("colors[i]")
You don't have any element with an id of colors[i]

YOu seem to be mixing up the id of the element with your array of colors.

Oh...and you do not need and should not be using a function in that code.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Old 11-06-2012, 03:40 AM   PM User | #3
rhollu
New to the CF scene

 
Join Date: Nov 2012
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
rhollu is an unknown quantity at this point
Quote:
Originally Posted by Old Pedant View Post
Makes no sense.

You have
Code:
<h1 id="colors">
but then you do
Code:
    document.getElementById("colors[i]")
You don't have any element with an id of colors[i]

YOu seem to be mixing up the id of the element with your array of colors.

Oh...and you do not need and should not be using a function in that code.
I tried a different approach but the "document.write" stopped working and all I got was 4 prompt boxes

Code:
<html>
<head>
<title>Task 2</title>
</head>

<body bgcolor="white">
<h1>
	<script type="text/javascript">
		// Comment Row.

		// This function will change the color of the text to the name of the color you give it.
			
			for (i=0; i < 4; i++)
			var colorText = window.prompt ("Enter a number from 0 to 4: ");
		{	if (colorText == "0")
			document.write("<font color=\""+colors[0]+"\">The color is "+colors[0]+".</font>");
			else if (colorText == "1")
			document.write("<font color=\""+colors[1]+"\">The color is "+colors[1]+".</font>");
			else if (colorText == "2")
			document.write("<font color=\""+colors[2]+"\">The color is "+colors[2]+".</font>");
			else if (colorText == "3")
			document.write("<font color=\""+colors[3]+"\">The color is "+colors[3]+".</font>");
			else if (colorText == "4")
			document.write("<font color=\""+colors[4]+"\">The color is "+colors[4]+".</font>");
                        else
                        document.write("The color is black.");
		}
		
		
		
		// Declare, create, and put values into your color array here
			var colors = new Array (5);
			colors[0] = "green";
			colors[1] = "blue";
			colors[2] = "yellow";
			colors[3] = "orange";
			colors[4] = "blue";
			
		// Create a loop that runs 4 times.
		// Each time it asks the user for a number between 0 and 4.
		// If an invalid input is entered, the color displayed should be black.
		// Otherwise display the color at that index in the array.
	</script>
</h1>
</body>
</html>
rhollu is offline   Reply With Quote
Old 11-06-2012, 07:27 AM   PM User | #4
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,033
Thanks: 197
Thanked 2,410 Times in 2,388 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
You cannot use document.write() more than once.

document.write() is in effect obsolete. document.write() statements must be run before the page finishes loading. Any document.write() statement that runs after the page finishes loading will create a new page and overwrite all of the content of the current page (including the Javascript which called it). So document.write() is at best really only useful to write the original content of your page. It cannot be used to update the content of your page after that page has loaded.

<font> tags ae also obsolete.

All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.
Philip M is offline   Reply With Quote
Old 11-06-2012, 08:38 AM   PM User | #5
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,033
Thanks: 197
Thanked 2,410 Times in 2,388 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Here you are (it does not work in IE as the handling of prompts within loops in IE is buggy and only the final text is displayed).

Code:
<html>
<head>
<title>Changing Font Color</title>
</head>

<body bgcolor="white">
<h1>
<span id="mytext">  The color is black. </span>
</hi>
<script type="text/javascript">

// Declare, create, and put values into your color array here (must be declared before they are referenced)
var colors = [];
colors[0] = "black";
colors[1] = "blue";
colors[2] = "yellow";
colors[3] = "orange";
colors[4] = "blue";
colors[5] = "green";
// Create a loop that runs 4 times.
// Each time it asks the user for a number between 0 and 5.
// If an invalid input is entered, the color displayed should be black.
// Otherwise display the color at that index in the array.

for (i=0; i < 4; i++) {
var c = window.prompt ("Enter a number from 0 to 5: ", "");
c = Number(c) || 0;  // trap NaN - default black
if (c >5) {c=0};  // trap entry over 5
var col = colors[c];
document.getElementById("mytext").innerHTML = "<font color=\""+col+"\">The color is "+col+".</font>";
}


</script>
</body>
</html>
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.

Last edited by Philip M; 11-06-2012 at 08:42 AM..
Philip M is offline   Reply With Quote
Old 11-08-2012, 01:03 PM   PM User | #6
u jayakodi
New Coder

 
Join Date: Sep 2010
Posts: 26
Thanks: 0
Thanked 0 Times in 0 Posts
u jayakodi is an unknown quantity at this point
jayakodiu@yahoo.com

The following code works fine with IE:

code:
<html>
<head>
<title>Changing Font Color</title> </head>
<body> <h1> <span id="mytext"> The color is black. </span> </hi>
</body>
<script type="text/javascript">
var colors = []; colors[0] = "black"; colors[1] = "blue"; colors[2] = "yellow"; colors[3] = "orange";
colors[4] = "blue"; colors[5] = "green";
for (i=0; i < 4; i++) {
var c = window.prompt ("Enter a number from 0 to 5: ", "");
c = Number(c) || 0; if (c >5) {c=0};
mytext.style.color=colors[c]
mytext.innerText="The color is "+colors[c]
}
</script>
</html>
u jayakodi is offline   Reply With Quote
Old 11-08-2012, 07:48 PM   PM User | #7
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,033
Thanks: 197
Thanked 2,410 Times in 2,388 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
The OP wanted that the script asked the user to input his choice 4 times (using a loop). Not just once.


mytext.style.color=colors[c]

Not sure if the OP had learned any CSS at this stage.
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.

Last edited by Philip M; 11-08-2012 at 07:50 PM..
Philip M is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:08 PM.


Advertisement
Log in to turn off these ads.