Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Mar 2006
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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);
    }

    }

  • #2
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    the bitwise comparision operator is ==

    if(rating ==1)
    ...
    else if(rating ==2)
    ...
    and so on
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #3
    Senior Coder Arbitrator's Avatar
    Join Date
    Mar 2006
    Location
    Splendora, Texas, United States of America
    Posts
    3,316
    Thanks
    29
    Thanked 279 Times in 273 Posts
    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>
    Last edited by Arbitrator; 03-08-2006 at 09:59 AM.
    For every complex problem, there is an answer that is clear, simple, and wrong.


  •  

    Posting Permissions

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