我想在地圖上設置一些隨機座標。我寫了console.log行來查看它是否成功地選取數字,但它告訴我,我的destinations
陣列中的所有內容都是NaN。JS:爲什麼我的變量被賦值而不是調用函數?
它看起來像是將Math.random函數分配給變量,而不是調用Math.random併爲其返回數字。任何人都知道發生了什麼?
var nodeCount = 10
var mapSize = 100;
var destinations = new Array(nodeCount);
for (var i = 0; i < nodeCount; i++) {
destinations[i] = new Array(2);
}
var generateDestinations = function(nodeCount) {
for (var i=0; i<nodeCount; i++) {
destinations[i][0] = Math.floor(Math.random*mapSize); //sets x-coord
destinations[i][1] = Math.floor(Math.random*mapSize); //sets y-coord
console.log(destinations);
}
};
generateDestinations(nodeCount);
要叫你以後需要括號功能函數名稱。 'Math.random()* mapSize' – nnnnnn 2014-08-29 03:28:36