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 10-05-2012, 09:15 PM   PM User | #1
assyrianlegend
New to the CF scene

 
Join Date: Oct 2012
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
assyrianlegend is an unknown quantity at this point
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!
assyrianlegend is offline   Reply With Quote
Old 10-05-2012, 09:19 PM   PM User | #2
WolfShade
Regular Coder

 
Join Date: Apr 2012
Location: St. Louis, MO, USA
Posts: 960
Thanks: 7
Thanked 100 Times in 100 Posts
WolfShade is an unknown quantity at this point
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".
WolfShade is offline   Reply With Quote
Old 10-05-2012, 09:21 PM   PM User | #3
assyrianlegend
New to the CF scene

 
Join Date: Oct 2012
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
assyrianlegend is an unknown quantity at this point
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.
assyrianlegend is offline   Reply With Quote
Old 10-05-2012, 09:45 PM   PM User | #4
WolfShade
Regular Coder

 
Join Date: Apr 2012
Location: St. Louis, MO, USA
Posts: 960
Thanks: 7
Thanked 100 Times in 100 Posts
WolfShade is an unknown quantity at this point
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".
WolfShade is offline   Reply With Quote
Old 10-05-2012, 10:17 PM   PM User | #5
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,578
Thanks: 62
Thanked 4,062 Times in 4,031 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
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.
Old Pedant is offline   Reply With Quote
Old 10-05-2012, 10:18 PM   PM User | #6
WolfShade
Regular Coder

 
Join Date: Apr 2012
Location: St. Louis, MO, USA
Posts: 960
Thanks: 7
Thanked 100 Times in 100 Posts
WolfShade is an unknown quantity at this point
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".
WolfShade is offline   Reply With Quote
Old 10-05-2012, 10:21 PM   PM User | #7
xelawho
Senior Coder

 
xelawho's Avatar
 
Join Date: Nov 2010
Posts: 2,461
Thanks: 52
Thanked 457 Times in 455 Posts
xelawho will become famous soon enoughxelawho will become famous soon enough
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.
xelawho is offline   Reply With Quote
Old 10-05-2012, 10:36 PM   PM User | #8
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,578
Thanks: 62
Thanked 4,062 Times in 4,031 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
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
Quote:
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.
Old Pedant is offline   Reply With Quote
Old 10-05-2012, 10:41 PM   PM User | #9
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,578
Thanks: 62
Thanked 4,062 Times in 4,031 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
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.
Old Pedant is offline   Reply With Quote
Old 10-05-2012, 11:11 PM   PM User | #10
xelawho
Senior Coder

 
xelawho's Avatar
 
Join Date: Nov 2010
Posts: 2,461
Thanks: 52
Thanked 457 Times in 455 Posts
xelawho will become famous soon enoughxelawho will become famous soon enough
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...
xelawho is offline   Reply With Quote
Old 10-05-2012, 11:17 PM   PM User | #11
VIPStephan
The fat guy next door


 
VIPStephan's Avatar
 
Join Date: Jan 2006
Location: Halle (Saale), Germany
Posts: 7,703
Thanks: 5
Thanked 875 Times in 850 Posts
VIPStephan is a jewel in the roughVIPStephan is a jewel in the roughVIPStephan is a jewel in the rough
May be that whole exercise is just to show how stupid this approach is?
__________________
Don’t click this link!
VIPStephan is offline   Reply With Quote
Old 10-06-2012, 12:25 AM   PM User | #12
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,578
Thanks: 62
Thanked 4,062 Times in 4,031 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
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.
Old Pedant is offline   Reply With Quote
Old 10-06-2012, 12:58 AM   PM User | #13
xelawho
Senior Coder

 
xelawho's Avatar
 
Join Date: Nov 2010
Posts: 2,461
Thanks: 52
Thanked 457 Times in 455 Posts
xelawho will become famous soon enoughxelawho will become famous soon enough
oh, dear.... maybe this is why I wasn't a very good student...

where does it say anything about *AND MODIFY*?
xelawho is offline   Reply With Quote
Old 10-06-2012, 01:07 AM   PM User | #14
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,578
Thanks: 62
Thanked 4,062 Times in 4,031 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
See...it says it right there:
Quote:
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.
Old Pedant is offline   Reply With Quote
Old 10-06-2012, 01:09 AM   PM User | #15
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,578
Thanks: 62
Thanked 4,062 Times in 4,031 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
(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.
Old Pedant 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 02:27 AM.


Advertisement
Log in to turn off these ads.