2

Okay, so I am just going over some basic programming tenets in JavaScript (I am new to programming, so bear with me please). Below is the code I am having issues with (pay particular attention to the string component of the array).

var name = new Array();
var sales = new Array();
var total = prompt("How many total salesmen does your department employ?");

for(i = 0; i < total; i++)
{
name[i] = prompt("What is his/her (the salesman's) name?");
sales[i] = prompt("How much (in dollars) did he/she sell?");
}
for(j = 0; j < i; j++)
{
document.write(name[j]+" had $"+ sales[j]+" in sales!<br>");
}

As you can see, it's just a simple code to get me familiar with arrays of various types of elements. For some reason, the code returns:

undefined had $100 in sales!
undefined had $2999 in sales!
undefined had $4999 in sales!
undefined had $32342 in sales!

which is exactly what I need, except for the fact that each of the elements within the name array is undefined.

I'm thinking it may be an issue with the document.write function, as i've read that it can be dubious but I still don't know exactly how to get this working properly.

Any help would be greatly appreciated!

user229044
  • 232,980
  • 40
  • 330
  • 338
Sam S.
  • 348
  • 1
  • 3
  • 15

3 Answers3

6

name is a property of window. Your function won't work at global scope. The simpler example which reproduces the problem would be...

var name = {}
name.blah = "blah"
document.write(name.blah); // undefined
      

Wrap it in a function, or choose a different variable name. names is more appropriate any way, since it's an array of multiple objects, you should choose a plural name for the variable:

var names = new Array();
var sales = new Array();
var total = prompt("How many total salesmen does your department employ?");

for(i = 0; i < total; i++)
{
names[i] = prompt("What is his/her (the salesman's) name?");
sales[i] = prompt("How much (in dollars) did he/she sell?");
}
for(j = 0; j < i; j++)
{
document.write(names[j]+" had $"+ sales[j]+" in sales!<br>");
}
user229044
  • 232,980
  • 40
  • 330
  • 338
  • Worked, fixed, and thanks for the thorough nature of your response. I knew it was something simple that I was overlooking. – Sam S. Dec 31 '14 at 02:49
2

I can reproduce the behavior you're describing in Chrome.

I think it's because of a subtlety with global variables in JavaScript. If you're in top-level code -- not inside a function -- and declare a variable, that's treated as a global variable, which means it's actually a property of the window object. There's already a property called window.name, which is predefined by the browser, so your attempt to make a global variable called name produces some unexpected behavior.

If you rename your name variable to names, it works as expected. (And since it's a bunch of names, names is a better name anyway.)

Alternatively, you could wrap the whole thing in a function and call it immediately. Then you have local variables (which really are variables), rather than global variables with their weird behavior:

(function() {
  var name = new Array();
  var sales = new Array();
  var total = prompt("How many total salesmen does your department employ?");

  for(i = 0; i < total; i++)
  {
    name[i] = prompt("What is his/her (the salesman's) name?");
    sales[i] = prompt("How much (in dollars) did he/she sell?");
  }
  for(j = 0; j < i; j++)
  {
    document.write(name[j]+" had $"+ sales[j]+" in sales!<br>");
  }
})();
Joe White
  • 94,807
  • 60
  • 220
  • 330
  • 1
    Thanks, since I am new to programming, I overlooked the possibility of a variable name overlapping with a window property. I can stop tearing out my hair now. :) – Sam S. Dec 31 '14 at 02:51
  • Yeah, that thing about global variables becoming `window` properties, instead of normal variables, is a big gotcha of JavaScript -- there are parts of JavaScript that are actually pretty clean and elegant, but this is emphatically not one of them. It's why a lot of libraries wrap all their code in a self-calling function like this. – Joe White Dec 31 '14 at 12:49
0

name refers to windows.name when javascript is run in browser.

Using another variable name fixes the problem

What is the `name` keyword in JavaScript?

Community
  • 1
  • 1
microtony
  • 192
  • 7