简体   繁体   English

JavaScript函数中变量的“未定义”结果

[英]'undefined' result for variable in Javascript function

something in my code is meaning that I am getting an array printed which contains only undefined values, not the random numbers I am expecting. 代码中的某些内容表示我正在打印一个数组,其中仅包含未定义的值,而不包含我期望的随机数。

My code is below: 我的代码如下:

 function List(max, min, numLists, numItems) { this.max = max, this.min = min, this.numLists = numLists, this.numItems = numItems, this.generateLists = function () { var fullArray = []; var completeArray = []; for (i = this.min; i < this.max; i++) { fullArray.push(i); } for (i = 0; i < numItems; i++) { var randomItem = Math.floor(Math.random() * (1 + (this.max - this.min))); completeArray.push(fullArray[randomItem]); } console.log(completeArray); } } var newList = new List(12, 100, 1, 15); newList.generateLists(); 

The code is supposed to print a random list of number between the min and max values. 该代码应该打印出一个介于最小值和最大值之间的随机数字列表。 I'm getting an array with 15 values in, but they are all undefined. 我得到一个包含15个值的数组,但是它们都是未定义的。 I'm guessing this mean there is something wrong with my first 'for' loop? 我猜这意味着我的第一个“ for”循环有问题吗?

If anyone has any suggestions on how I could make this better please do criticise! 如果有人对我如何做得更好有任何建议,请提出批评!

Thanks in advance. 提前致谢。

You have min and max mixed up in your arguments list. 您的参数列表中混合了minmax this results in an impossible boundary for your numbers (greater than 100 but less than 12). 这将导致您的数字无法达到界限(大于100但小于12)。 Just swap the parameters in the first line from max,min to min,max . 只需将第一行中的参数从max,min交换到min,max

 function List(min,max,numLists,numItems){ this.max = max, this.min = min, this.numLists = numLists, this.numItems = numItems, this.generateLists = function (){ var fullArray = []; var completeArray = []; for ( i = this.min ; i<this.max ; i++) { fullArray.push(i); } for ( i = 0 ; i<numItems ; i++) { var randomItem = Math.floor(Math.random() * (1+(this.max-this.min))); completeArray.push(fullArray[randomItem]); } console.log(completeArray); } } var newList = new List ( 12 , 100 , 1,15); newList.generateLists(); 

I think you swap the position of max and min when you initiate newList . 我认为您在启动newList时交换了max和min的位置。

Change the line to: 将行更改为:

var newList = new List ( 100, 12 , 1,15);

Then it should work fine. 然后它应该工作正常。

You were pushing fullArray[randomItem] that contains nothing. 您正在推送fullArray[randomItem]包含任何内容的fullArray[randomItem] It is never initialized 永远不会初始化

 function List(max, min, numLists, numItems) { this.max = max, this.min = min, this.numLists = numLists, this.numItems = numItems, this.generateLists = function() { var completeArray = []; for (i = 0; i < numItems; i++) { var randomItem = Math.floor(Math.random() * (1 + (this.max - this.min))); completeArray.push(randomItem); } document.write(completeArray); } } var newList = new List(12, 100, 1, 15); newList.generateLists(); 

I think maybe you max and min arguments were in the wrong order. 我认为也许您的max和min参数的顺序错误。 You were trying to access the fullArray with a negative index due to subtracting a larger number from a smaller one. 您正在尝试使用负索引访问fullArray ,因为要从较小的索引中减去较大的数字。

 function List(min,max,numLists,numItems){ this.max = max, this.min = min, this.numLists = numLists, this.numItems = numItems, this.generateLists = function (){ var fullArray = []; var completeArray = []; for ( i = this.min ; i<this.max ; i++) { fullArray.push(i); } for ( i = 0 ; i<numItems ; i++) { var randomItem = Math.floor(Math.random() * (1+(this.max-this.min))); console.log(randomItem) completeArray.push(fullArray[randomItem]); } console.log(completeArray); } } var newList = new List ( 12 , 100 , 1,15); newList.generateLists(); 

I think max and min parameter are swapped 我认为max和min参数已交换

This 这个

function List(max,min,numLists,numItems){

should be 应该

function List(min,max,numLists,numItems){

Just replace this line; 只需替换此行即可;

var randomItem = Math.floor(Math.random() * (1+(this.max-this.min)));

with; 与;

var randomItem = Math.floor(Math.random()*(fullArray.length)+1);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM