好吧,我只是在回顧JavaScript中的一些基本編程原則(我是編程新手,請耐心等待)。下面是我遇到的問題(特別注意數組的字符串組件)。JavaScript數組問題
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>");
}
正如你所看到的,它只是一個簡單的代碼來讓我熟悉各類元素的數組。出於某種原因,代碼返回:
undefined had $100 in sales!
undefined had $2999 in sales!
undefined had $4999 in sales!
undefined had $32342 in sales!
這正是我需要的,除了一個事實,即每名數組中的元素是不確定的。
我在想這可能是document.write函數的問題,因爲我讀過它可能是可疑的,但我仍然不知道如何正確地工作。
任何幫助將不勝感激!
工作得很好,對我來說 - > ** HTTP://jsfiddle.net/k52betc3/** – adeneo
@adeneo那的jsfiddle被打破了我Chrome – meagar
@adeneo me too im正在運行版本號39.0.2171.95 m Chrome –