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

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?

10-05-2012, 09:21 PM

assyrianlegend

Quote:

Originally Posted by WolfShade

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.

10-05-2012, 09:45 PM

WolfShade

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.

10-05-2012, 10:17 PM

Old Pedant

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.

10-05-2012, 10:18 PM

WolfShade

Quote:

Originally Posted by Old Pedant

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. :\

10-05-2012, 10:21 PM

xelawho

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...

And then, of course, your find that Expression includes PrimaryExpression. And so on.

10-05-2012, 10:41 PM

Old Pedant

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.

10-05-2012, 11:11 PM

xelawho

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

May be that whole exercise is just to show how stupid this approach is? :D

10-06-2012, 12:25 AM

Old Pedant

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.

10-06-2012, 12:58 AM

xelawho

oh, dear.... maybe this is why I wasn't a very good student...

where does it say anything about *AND MODIFY*?

10-06-2012, 01:07 AM

Old Pedant

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.

10-06-2012, 01:09 AM

Old Pedant

(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.)