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 ponent 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>");
}
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 ponent 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!
name
is your problem, and also the reason it works for some people in JSFiddle. JSFiddle wraps your code in a function before its invoked.
– user229044
♦
Commented
Dec 31, 2014 at 2:39
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>");
}
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>");
}
})();
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?