0

I don't think I'm fully grasping how this works (I'm a little embarrassed...by a little I mean a lot). Basically this is supposed to create a prompt and write console.log fullName with the first two letters in each prompt to be capitalized and concatenate together. Please help!

var fullName = ""; //Why does fullName have to be defined as a string? and when it's removed it doubles the value? var name; var firstLetter; var fixName = function () { firstLetter = name.substring(0, 1); name = firstLetter.toUpperCase() + name.substring(1); fullName = fullName + " " + name; //what exactly is happening here under the fullName variable? What value is passing into fullName after it's being called? } name = prompt("Enter your first name (all in lower case):"); fixName(); name = prompt("Enter your second name (all in lower case):"); fixName(); console.log("And your fullname is:" + fullName); 
3
  • 3
    Just FYI: That's a pretty terrible way to do this. :) Commented Jan 17, 2012 at 3:08
  • 1
    It is one of the code year exercise to understand local and global variables, that's why it is awful. Commented Jan 17, 2012 at 3:11
  • Oh I know haha...but it's for the Codecademy classes that are online. Unfortunately this is the only site that I know of that has JS exercises. More importantly for me right now is understanding why this is working the way it does? haha Commented Jan 17, 2012 at 3:11

4 Answers 4

2

Here's an annotated version of the function:

var fixName = function () { // get the first letter of the string firstLetter = name.substring(0, 1); // assign back to name the uppercased version of the first letter // with the rest of the name name = firstLetter.toUpperCase() + name.substring(1); // add name onto the end of fullName // this will accumulate each time this function is called because // fullname is a global variable so it will get longer and longer each time // with more and more names in it fullName = fullName + " " + name; } 

FYI, this is pretty horrible code overall. It should be using at least some local variables and a function argument like this:

var fullName = ""; function fixName(name) { var firstLetter = name.substring(0, 1); fullName = fullName + " " + firstLetter.toUpperCase() + name.substring(1); } fixName(prompt("Enter your first name (all in lower case):")); fixName(prompt("Enter your second name (all in lower case):")); console.log("And your fullname is:" + fullName); 

It probably shouldn't be modifying a global variable as a side effect either (probably should use a return value), but I didn't change that.

Sign up to request clarification or add additional context in comments.

2 Comments

Wow...I can't believe I didn't see that. fullName adds an empty variable and a space before it gets to a filled value...and then repeats. This really IS horrible code! I'm doing my best and have been pretty committed to learning this language but I feel like I'm not going to get any better if the exercises I find are broken/deprecated/or overall bad.
I really appreciate the quick response. This is quickly becoming my new favorite site!
0

fullName Is first defined as an empty string, to which name gets concatenated once name has been changed to have its first letter uppercased.

In each call of prompt, the returned value is stored to name, which is also a global variable. That value of name is changed to proper case inside fixName().

Since the function fixName() is called twice and fullName is declared in the global scope, the output of fixName() is added to the variable fullName both times. Fist for the first name, and second for the surname (second name). In the end, fullName should contain Firstname Lastname (with an extra space at the beginning).

Comments

0

The code is ugly, using so-called "side-effects", operating on global variables, without function parameters or function result. It fills fullName with space, capitalized first name, space, capitalized second name.

Using the following would make the global variables unnecessary.

function capitalized(s) { if (s == "") return ""; return s.substring(0, 1).toUpperCase() + s.substring(1); } 

Comments

0

To adapt your original code:

window.onload = function(){ var names = {}, result = ''; function fixName(input){ return input.substr(0, 2).toUpperCase() } names.first = window.prompt("What is your first name?") names.last = window.prompt("What is your last name?") for(var n in names){ var nameValue = names[n]; result += fixName(nameValue) } console.log(result) } 

There are far better ways as others will illustrate, but this is based on what you wrote. Happy Javascripting :)

1 Comment

FYI, there is no guaranteed order when iterating properties of an object like this for(var n in names) so this isn't guaranteed to get first name before last name.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.