Page 1 of 2 12 LastLast
Results 1 to 10 of 14
  1. #1

    Default Whats wrong with the code?


    guyz new problem...i thought it would be better if i reuse this thread to lessen the flames i get...hehehe...joke...i know all of it is constructive criticism...need to ask about a part of my code...basically it goes like this user inputs a word and the program figures out if its a palindrome or not....finish all of that and it works perfectly but what i want is to display an image rather than text to say if its palindrome or not... what im using write now is getelementbyid("output").innerhtml="it is a palindrome" and want to replace it with getelementbyid(output).innerhtml="<img src="file.jpg">" but it doesnt work...im really confused why it wouldnt.....any help would be appreciated...

    ps. ''output'' is the id for the div tag i use below....ill post the code if anyone thinks it would help

  2. #2
    Elite Member
    Join Date
    Aug 2008
    Posts
    1,053
    Blog Entries
    1
    surely won't work.... (If i still remember my javascript knowledge)

    1. check your input checkbox
    2. you need to parse the calculator.input1.value
    3. if (document.getElementById("Add").checked==True) is obviously useless!!!
    4. you gotta used isNaN and the rest, make good and simple algorithm for that.
    Last edited by MarkCuering; 01-18-2009 at 11:59 AM. Reason: Added item no. 4

  3. #3
    Infractions: 0/3 (6)
    Join Date
    Sep 2006
    Gender
    Male
    Posts
    7,290
    i have no idea about javascript but one thing i notice is that your input type = text.

    can you mathematically process text values?
    i mean, same identifier ra na siya if number imong input?
    ang amo man gud ancient programming language sa una he he you can only add integers.
    basin modern na ron.

    well, maybe in javascript, i don't know.

  4. #4
    Quote Originally Posted by MarkCuering View Post
    surely won't work.... (If i still remember my javascript knowledge)

    1. check your input checkbox
    2. you need to parse the calculator.input1.value
    3. if (document.getElementById("Add").checked==True) is obviously useless!!!
    4. you gotta used isNaN and the rest, make good and simple algorithm for that.
    1. what wrong with it?
    2. oh yeah forgot about that one but it should still have displayed a string sum like ah 8 + 8 = 88
    3. useless? im sorry im not folowing...i was just introduced to javascipt so i really dont know why
    4. isnan? (goes and google it)

  5. #5
    MarkCuering is correct, you have to parse your values, and if (document.getElementById("Add").checked==True) is obviously useless!!! If I may, let me add a few points to what he said. In general JavaScript is almost identical to C Language, so if you are familiar with C, JavaScript is pretty plain for you. However JavaScript follows certain convention like:

    1. When you create a function it should be is lower case, otherwise it will not be recognized.
    2. When you use a variable, you should declare it for documentation purposes. Although in most cases, it will be declared automatically.

    the code below is working, but there is a better code out there somewhere.

    Code:
    <html>
    <head>
    <title>Another Assignment Code</title>
    <script type="text/javascript">
    function add() {
    var x=parseInt(calculator.input1.value);
    var y=parseInt(calculator.input2.value);
    var sum= x + y;
    
    document.getElementById("output").innerHTML= sum;
    }
    </script>
    </head>
    
    <body>
    <form name="calculator">
    <input type=checkbox id="Add" onClick="add()">
    <input type=text name="input1">
    <input type=text name="input2">
    <div id="output"></div>
    </form>
    </body>
    </html>
    Give yourself a pat in the back, another assignment solved!
    Last edited by ChaosOrb; 01-18-2009 at 08:23 PM.

  6. #6
    Oh damn my professor never told me about the lower case thing in doing functions (grr)...yup i already know c# so some of the areas are easy but have to work on the conventions and the if statement was his idea..he told us to use an if statement in the function so i thought that would do the trick...hmmm

  7. #7
    Quote Originally Posted by Deathnote View Post
    Oh damn my professor never told me about the lower case thing in doing functions (grr)...yup i already know c# so some of the areas are easy but have to work on the conventions and the if statement was his idea..he told us to use an if statement in the function so i thought that would do the trick...hmmm
    still you need a condition.
    that snippet given still would calculate ig unchecked sa checkbox.

  8. #8
    Elite Member
    Join Date
    Aug 2008
    Posts
    1,053
    Blog Entries
    1
    Quote Originally Posted by ChaosOrb View Post
    In general JavaScript is almost identical to C Language, so if you are familiar with C, JavaScript is pretty plain for you.
    Bro, I Agree… but there are a lot of pitfalls, if you don’t have such discipline in writing your code, and some experience in document parsing.

    Quote Originally Posted by ChaosOrb View Post
    1. When you create a function it should be is lower case, otherwise it will not be recognized.
    This time, I wont… hehe… No…not all the time, and not restricted to…. What happen here is the time he writes this way:

    <input type=checkbox Id="Add" onClick="Add()">

    And call javascript:

    if (document.getElementById("Add").checked==True)

    wHAaaaaat? did JavaScript parse it both as 'Add' Well… there are a lot of ways to solve having the same function name and id but however, the easiest and fastest way is to apply discipline, you as a programmer… by adding two pre/postfix char or whatever naming rules you’d like to apply.

    Eg. IdSum and fnSum (I got two characters here as my ident.)

    Quote Originally Posted by Deathnote View Post
    1. what wrong with it?
    2. oh yeah forgot about that one but it should still have displayed a string sum like ah 8 + 8 = 88
    4. isnan? (goes and google it)
    It’s because you are adding two strings, not two integers… yeah..google the isnan its very important.

    Quote Originally Posted by moz_k2 View Post
    still you need a condition.
    that snippet given still would calculate ig unchecked sa checkbox.
    You’re right… but I won’t suggest it that way… instead I will pass an object to my function by using 'this' and directly manipulate it, by calling its method. there is a very big difference by calling an object METHOD, and running different FUNCTION (which requires different kinds of process)

    Here’s my sample code… Glad if someone explained hehehe coz its coffee time here…

    PHP Code:
    <html>
    <
    head
    <
    script type="text/javascript">
    function 
    fnAdd(val){
        if (
    val.checked) {
        
    sum =parseFloat(calculator.input1.value) + parseFloat(calculator.input2.value);
        if (!
    isNaN(sum)) document.getElementById("output").innerHTMLsum
        
    else document.getElementById("output").innerHTML"Invalid data."
            
    }
        else {
            
    document.getElementById("output").innerHTML""
            
    }
    }
    </script>

    </head>
    <body>
    <form name=calculator>
    <input type=checkbox Id="idAdd" onClick="fnAdd(this)">
    <input type=text name=input1>
    <input type=text name=input2>
    <div id=output> </div>
    </form>
    </body>
    </html> 
    val.checked vs. document.getElementById("Add").checked==True)

    Which one is better? I'm not web guy anyway...

    Hope this may help...



  9. #9
    Elite Member
    Join Date
    Aug 2008
    Posts
    1,053
    Blog Entries
    1
    by the way, I used parseFloat so it allows to add floating point numbers.

  10. #10
    Quote Originally Posted by MarkCuering View Post
    Bro, I Agree… but there are a lot of pitfalls, if you don’t have such discipline in writing your code, and some experience in document parsing.



    This time, I wont… hehe… No…not all the time, and not restricted to…. What happen here is the time he writes this way:

    <input type=checkbox Id="Add" onClick="Add()">

    And call javascript:

    if (document.getElementById("Add").checked==True)

    wHAaaaaat? did JavaScript parse it both as 'Add' Well… there are a lot of ways to solve having the same function name and id but however, the easiest and fastest way is to apply discipline, you as a programmer… by adding two pre/postfix char or whatever naming rules you’d like to apply.

    Eg. IdSum and fnSum (I got two characters here as my ident.)



    It’s because you are adding two strings, not two integers… yeah..google the isnan its very important.



    You’re right… but I won’t suggest it that way… instead I will pass an object to my function by using 'this' and directly manipulate it, by calling its method. there is a very big difference by calling an object METHOD, and running different FUNCTION (which requires different kinds of process)

    Here’s my sample code… Glad if someone explained hehehe coz its coffee time here…

    PHP Code:
    <html>
    <
    head
    <
    script type="text/javascript">
    function 
    fnAdd(val){
        if (
    val.checked) {
        
    sum =parseFloat(calculator.input1.value) + parseFloat(calculator.input2.value);
        if (!
    isNaN(sum)) document.getElementById("output").innerHTMLsum
        
    else document.getElementById("output").innerHTML"Invalid data."
            
    }
        else {
            
    document.getElementById("output").innerHTML""
            
    }
    }
    </script>

    </head>
    <body>
    <form name=calculator>
    <input type=checkbox Id="idAdd" onClick="fnAdd(this)">
    <input type=text name=input1>
    <input type=text name=input2>
    <div id=output> </div>
    </form>
    </body>
    </html> 
    val.checked vs. document.getElementById("Add").checked==True)

    Which one is better? I'm not web guy anyway...

    Hope this may help...



    oh this is definitely better...tnx bro...so you can have a function with a capital first letter but not have the same id and name... i thought Add and Add() was different...damn....oh yeah found out that isnan is to check if it is string or not..wow im learning alot from you guyz


    but last question i promise...where did val come from? i mean its neither the id nor the name of the check box yet it was used that way...does the control that called the function when you put val on the parameter becomes the value for val? tnx

  11.    Advertisement

Page 1 of 2 12 LastLast

Similar Threads

 
  1. WHAT'S WRONG WITH THE WORLD TODAY? Let's fight for righteousness
    By illuminatus in forum General Discussions
    Replies: 13
    Last Post: 05-03-2012, 08:00 AM
  2. WHATS WRONG WITH MY PC- upgrading the rig and its downside
    By benchkicker in forum Computer Hardware
    Replies: 74
    Last Post: 09-09-2007, 12:47 AM
  3. whats wrong with my photoshop?
    By estor_boot in forum Software & Games (Old)
    Replies: 10
    Last Post: 07-25-2007, 02:46 PM
  4. Whats wrong with my CD-rom?
    By pobre in forum Computer Hardware
    Replies: 8
    Last Post: 11-30-2005, 07:47 PM
  5. whats wrong with my monitor?
    By jonmlas in forum Computer Hardware
    Replies: 8
    Last Post: 10-22-2005, 02:43 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
about us
We are the first Cebu Online Media.

iSTORYA.NET is Cebu's Biggest, Southern Philippines' Most Active, and the Philippines' Strongest Online Community!
follow us
#top