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.
Page 1 of 2 12 LastLast
Results 1 to 15 of 19
  1. #1
    New to the CF scene
    Join Date
    Oct 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Javascript help! i am a noob!!!

    hello so i am taking a javascript class and the teacher asked us to code the fallowing :

    Write an entire function named Bold_Name, it should:
    Build an HTML string by placing the global variable fullname in between the BOLD tags <B> and </B>
    Here is an example of what this string will contain: <B> ... </B>
    Use the Javascript return statement to send the string back to the caller
    Do not call Bold_Name, or initialize fullname, it will be done for you.
    Hint: use the '+' operator to create the string


    My code is :

    function Bold_Name() {
    fullname += "<b>";
    fullname += fullname;
    fullname += "</b>";
    return(fullname);

    However when i put the code in it says its wrong ( online class)

    please help!

  • #2
    Regular Coder
    Join Date
    Apr 2012
    Location
    St. Louis, MO
    Posts
    985
    Thanks
    7
    Thanked 101 Times in 101 Posts
    You aren't declaring a global variable called fullname, much less giving it any value.

    HOW is the function being called, HOW is the global variable being initialized?
    ^_^

    If anyone knows of a website that can offer ColdFusion help that isn't controlled by neurotic, pedantic jerks* (stackoverflow.com), please PM me with a link.
    *
    The neurotic, pedantic jerks are not the owners; just the people who are in control of the "popularity contest".

  • #3
    New to the CF scene
    Join Date
    Oct 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by WolfShade View Post
    You aren't declaring a global variable called fullname, much less giving it any value.

    HOW is the function being called, HOW is the global variable being initialized?
    well we have to copy paste the code into the website, and the website says not to declare " fullname" and also i dont think we have to call it because the teacher says that will be done by the program it self.

  • #4
    Regular Coder
    Join Date
    Apr 2012
    Location
    St. Louis, MO
    Posts
    985
    Thanks
    7
    Thanked 101 Times in 101 Posts
    If that is the case, then the only thing that I think needs doing is to change return(fullname); to return fullname;

    At least, as far as I can tell.
    ^_^

    If anyone knows of a website that can offer ColdFusion help that isn't controlled by neurotic, pedantic jerks* (stackoverflow.com), please PM me with a link.
    *
    The neurotic, pedantic jerks are not the owners; just the people who are in control of the "popularity contest".

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,602
    Thanks
    78
    Thanked 4,387 Times in 4,352 Posts
    No, that would make no difference, WolfShade.

    JavaScript, in common with virtually all modern languages, will treat fullname and ( fullname ) the same.

    The general syntax for an expression is something like:
    Code:
    expression :: variable | constant | ( expression }
    and so on.

    That's needed, so that you can build up expressions such as
    Code:
        return ( ("xxfullnam" ).substring(2) + "e");
    (somewhat silly, but you get the idea.

    *********

    The only thing I see missing is the } needed at the end of the function.
    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.

  • #6
    Regular Coder
    Join Date
    Apr 2012
    Location
    St. Louis, MO
    Posts
    985
    Thanks
    7
    Thanked 101 Times in 101 Posts
    Quote Originally Posted by Old Pedant View Post
    The only thing I see missing is the } needed at the end of the function.
    I just assumed it was accidentally left out of the copy/paste process. :\
    ^_^

    If anyone knows of a website that can offer ColdFusion help that isn't controlled by neurotic, pedantic jerks* (stackoverflow.com), please PM me with a link.
    *
    The neurotic, pedantic jerks are not the owners; just the people who are in control of the "popularity contest".

  • #7
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,969
    Thanks
    56
    Thanked 557 Times in 554 Posts
    I think you're going to run into troubles constructing a string like that using the variable as a based. Have a look at it with alerts to show you what is going on...

    Code:
    function Bold_Name() {
    fullname += "<b>";
    alert(fullname)
    fullname += fullname;
    alert(fullname)
    fullname += "</b>";
    alert(fullname)
    return fullname;
    }
    remember, the function doesn't have to return fullname - it can return anything you like.

  • #8
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,602
    Thanks
    78
    Thanked 4,387 Times in 4,352 Posts
    If you care, look here:
    http://hepunx.rl.ac.uk/~adye/jsspec11/llr.htm

    Easiest way to read that is to start backwards. So the first thing you see is
    PrimaryExpression:
    ( Expression )
    Identifier
    IntegerLiteral
    FloatingPointLiteral
    StringLiteral
    false
    true
    null
    this
    And then, of course, your find that Expression includes PrimaryExpression. And so on.
    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.

  • #9
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,602
    Thanks
    78
    Thanked 4,387 Times in 4,352 Posts
    The whole thing is kind of an insane exercise.

    Xelawho is of course right.

    If you come into the function with fullname set to (example only) "zamboni".

    Then at the end of the function the result will be that fullname will contain "zamboni<b>zamboni<b></b>".

    The only way to do this incredibly stupid question is directly:
    Code:
    function Bold_Name() 
    {
        fullname = "<b>" + fullname + "</b>";
    }
    It's almost impossible to think of a WORSE way to write JavaScript code. Or code in *ANY* language, for that matter.

    Whoever designed that course should be taken out and shot.
    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.

  • #10
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,969
    Thanks
    56
    Thanked 557 Times in 554 Posts
    I don't think it's that stupid (at least in the context of all the other stupid homework assignments we see).

    There's nothing in the question that says you have to build the string using +=, so the direct method would certainly apply. You could even, if you were feeling racy, just return that:
    return "<b>"+fullname+"</b>";

    because there's nothing to say that the function has to return fullname, just that it has to use it. If you wanted to build a string in the way that assyrianlegend was, you just as easily could do
    Code:
    function Bold_Name() {
    var str=""
    str += "<b>";
    str += fullname;
    str += "</b>";
    return str;
    }
    Anyway. I guess 3 answers to one question is enough...

  • #11
    The fat guy next door VIPStephan's Avatar
    Join Date
    Jan 2006
    Location
    Halle (Saale), Germany
    Posts
    8,680
    Thanks
    6
    Thanked 1,009 Times in 982 Posts
    May be that whole exercise is just to show how stupid this approach is?

  • #12
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,602
    Thanks
    78
    Thanked 4,387 Times in 4,352 Posts
    Xelawho: Read his homework description again. The function is supposed to use *AND MODIFY* the global variable fullname. *THAT* is why it is so stupid.

    The "framework" that will test his code is not even going to LOOK at the return value from the function. And idiotic framework written, as VIPStephan says, to illustrate the worst possible way to do things. Makes sense to me. NOT.
    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.

  • #13
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,969
    Thanks
    56
    Thanked 557 Times in 554 Posts
    oh, dear.... maybe this is why I wasn't a very good student...

    where does it say anything about *AND MODIFY*?

  • #14
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,602
    Thanks
    78
    Thanked 4,387 Times in 4,352 Posts
    See...it says it right there:
    Use the Javascript return statement to send the string back to the caller
    SHEESH. Some people need new glasses.

    I don't. I just need a new brain.
    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.

  • #15
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,602
    Thanks
    78
    Thanked 4,387 Times in 4,352 Posts
    (Okay...it's still stupid to work on a global, instead of on a passed in value, but never mind. Xelawho's answer is clearly what was expected. The function shouldn't modifiy the value of fullname in any way.)
    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.


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

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