assyrianlegend 10-05-2012, 09:15 PM 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!
WolfShade 10-05-2012, 09:19 PM 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?
assyrianlegend 10-05-2012, 09:21 PM 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.
WolfShade 10-05-2012, 09:45 PM 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.
Old Pedant 10-05-2012, 10:17 PM 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:
expression :: variable | constant | ( expression }
and so on.
That's needed, so that you can build up expressions such as
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.
WolfShade 10-05-2012, 10:18 PM 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. :\
xelawho 10-05-2012, 10:21 PM 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...
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.
Old Pedant 10-05-2012, 10:36 PM 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.
Old Pedant 10-05-2012, 10:41 PM 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:
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.
xelawho 10-05-2012, 11:11 PM 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
function Bold_Name() {
var str=""
str += "<b>";
str += fullname;
str += "</b>";
return str;
}
Anyway. I guess 3 answers to one question is enough...
VIPStephan 10-05-2012, 11:17 PM May be that whole exercise is just to show how stupid this approach is? :D
Old Pedant 10-06-2012, 12:25 AM 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.
xelawho 10-06-2012, 12:58 AM oh, dear.... maybe this is why I wasn't a very good student...
where does it say anything about *AND MODIFY*?
Old Pedant 10-06-2012, 01:07 AM 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.
Old Pedant 10-06-2012, 01:09 AM (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.)
Logic Ali 10-06-2012, 01:20 AM The "framework" that will test his code is not even going to LOOK at the return value from the function.
Without seeing it's source code, how can you possibly know that?
However unnecessary in the circumstances, the requirement was to use return, so it could be critical.
Old Pedant 10-06-2012, 01:51 AM Read my prior answer about needing a new brain.
I flat out misread the original post.
Philip M 10-06-2012, 11:04 AM You seem to have got your knickers in a twist. It was a simple enough question. Surely what the OP is looking for was (as xelawho has said)
<script type="text/javascript">
var fullname = ""; // fullname declared outside the function is a global variable
function Bold_Name(which) {
fullname = "<b>";
fullname += which;
fullname += "</b>";
return fullname;
}
alert (Bold_Name("Philip"));
</script>
xelawho 10-06-2012, 05:03 PM oh, my. I'm beginning to see another reason why we shouldn't be doing homework questions. From the instructions:
Do not ... initialize fullname, it will be done for you.
so this:
var fullname = "";
will wipe out the value of the established variable. ie, FAIL
there's nothing about Bold_Name accepting an argument, so this:
function Bold_Name(which)
will also naturally FAIL
the simplest answer I can see here is
function Bold_Name() {
return "<b>"+fullname+"</b>";
}
slightly fancier being
function Bold_Name() {
var str = "<b>";
str += fullname;
str += "</b>";
return str;
}
|