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 03-08-2006, 08:07 AM   PM User | #1
warwind10
New to the CF scene

 
Join Date: Mar 2006
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
warwind10 is an unknown quantity at this point
Undefined error next to the picture

I used this function but it says undefined next to the picture. Please help a newbie~~


function convertRating(rating)

{
var pic;

pic = "<img src=\"star.gif\" height=20 />";



if (rating = 1)
{

document.write(pic);

}

else if (rating = 2)
{
document.write(pic + pic);
}

else if (rating = 3)

{
document.write(pic + pic + pic);
}

else if (rating = 4)

{
document.write(pic + pic + pic + pic);
}

else if (rating = 5)

{
document.write(pic + pic + pic + pic + pic);
}

}
warwind10 is offline   Reply With Quote
Old 03-08-2006, 09:11 AM   PM User | #2
Kor
Red Devil Mod


 
Kor's Avatar
 
Join Date: Apr 2003
Location: Bucharest, ROMANIA
Posts: 8,478
Thanks: 58
Thanked 379 Times in 375 Posts
Kor has a spectacular aura aboutKor has a spectacular aura about
the bitwise comparision operator is ==

if(rating ==1)
...
else if(rating ==2)
...
and so on
__________________
KOR
Offshore programming
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
Kor is offline   Reply With Quote
Old 03-08-2006, 09:50 AM   PM User | #3
Arbitrator
Senior Coder

 
Arbitrator's Avatar
 
Join Date: Mar 2006
Location: Splendora, Texas, United States of America
Posts: 2,905
Thanks: 5
Thanked 189 Times in 186 Posts
Arbitrator is on a distinguished road
In other words, your original code should look something like this:

Code:
<script type="text/javascript">

function convertRating(rating) {

var pic = "<img src=\"star.gif\" height=\"20\"\/>";

if (rating == 1) {
  document.write(pic);
  }
else if (rating == 2) {
  document.write(pic + pic);
  }
else if (rating == 3) {
  document.write(pic + pic + pic);
  }
else if (rating == 4) {
  document.write(pic + pic + pic + pic);
  }
else if (rating == 5) {
  document.write(pic + pic + pic + pic + pic);
  }

}

</script>
However, using a "for" statement can simplify things a bit since you're essentially doing the same thing over and over:

Code:
<script type="text/javascript">

function convertRating(rating) {

for(i = rating; i > 0; i--) {
  document.write("<img style=\"height: 20px; width: 20px;\"");
  document.write(" title=\"" + rating + " Stars\"");
  document.write(" alt=\"" + rating + " Stars\"");
  document.write(" src=\"star.gif\"\/>");
  }

}

</script>
__________________
Please for the love of god stop making IE. You current "browser"s cause me to cry every day. —Phil *

Last edited by Arbitrator; 03-08-2006 at 09:59 AM..
Arbitrator is online now   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 08:36 AM.


Advertisement
Log in to turn off these ads.