我是JS新手,並從CodeAcademy工作中創建了這個原始問題。現在我想將我的羊羣放入一個物體中,並使用我的綿羊計數器功能訪問它。我是新來的訪問對象的鍵/值,並堅持我做錯了什麼。提前致謝!使用javascript訪問函數中的對象
Original Code
var sheepCounter = function (numSheep, monthNumber, monthsToPrint) {
for (monthNumber = monthNumber; monthNumber <= monthsToPrint; monthNumber++) {
numSheep *= 4;
console.log("There will be " + numSheep + " sheep after " + monthNumber + " month(s)!");
}
return numSheep;
}
New Code:
var flock = {
sheep: 4,
month: 1,
totalMonths: 12
};
var sheepCounter = function (counter) {
for (counter[month] = counter[month]; counter[month] <= counter[totalMonths]; counter[month]++) {
numSheep *= 4;
console.log("There will be " + counter[sheep] + " sheep after " + counter[month] + " month(s)!");
}
return counter[sheep];
}
您能否請您清楚解釋問題? – thefourtheye