JS递归函数:没有递归时工作正常,使用递归时返回答案加“未定义”

时间:2016-07-02 21:14:19

标签: javascript arrays recursion

我目前正在使用以下递归函数。它的作用是将3个项目分配给3个“扇区”,使用递归重新运行该函数,直到每个项目都分配给它自己的扇区。当第一次尝试将每个项目分配给空插槽时,它可以正常工作2/9次,但如果该函数使用递归,则会显示(当我显示时,使用console.log,应该返回的变量)首先是正确的坐标,然后是一个数组[undefined,undefined],它似乎覆盖了正确的坐标作为结果变量。然后该函数返回[undefined,undefined]。

下面是我的console.log输出的示例。如您所见,仅在多次调用函数时才会发生未定义的结果。奇怪的是,在这些情况下,函数确实起作用(起初) - 将island_core分配给实际坐标 - 然后用[undefined,undefined]替换那些工作坐标。我的代码显示在控制台输出下方。

任何人都知道如何解决这个问题?非常感谢您提供的任何帮助!

Console.log输出:

ASSIGNING ISLAND 1
Island sector distributor: 0.4996039977514938
Water top full: false
Water mid full: false
Water bottom full: false
Assigned sector mid
Island core: 145.7255743052315, 628.1824251323584
ASSIGNING ISLAND 2
Island sector distributor: 0.4175444925572739
Water top full: false
Water mid full: true
Water bottom full: false 
Island sector distributor: 0.9873914243694555 
Water top full: false
Water mid full: true 
Water bottom full: false 
Assigned sector bottom 
Island core: 227.29271060663325, 819.4032413102415
Island core: undefined, undefined
undefined
ASSIGNING ISLAND 3
Island sector distributor: 0.8861211980050283 
Water top full: false 
Water mid full: true
Water bottom full: true
Island sector distributor: 0.8407451988967638 
Water top full: false 
Water mid full: true
Water bottom full: true
Island sector distributor: 0.32777241987116223
Water top full: false
Water mid full: true
Water bottom full: true
Assigned sector top
Island core: 207.65905036573935, 173.1130653613051
Island core: undefined, undefined

代码(每个赋值语句中的代码似乎都有效 - 它只是在一个范围内随机分配一组坐标)。

var water_top_full = false;
var water_mid_full = false;
var water_bottom_full = false;

//Make a recursive function that assigns islands to sectors,
//and reassigns if an island is assigned to a full sector.

var assign_island_sector = function(){
	var island_sector_distributor = Math.random();
	//Object for island_core
	var island_core = [];
	console.log("Island sector distributor: " + island_sector_distributor);
	console.log("Water top full: " + water_top_full);
	console.log("Water mid full: " + water_mid_full);
	console.log("Water bottom full: " + water_bottom_full);
	if (island_sector_distributor <= (1/3)){
		if (water_top_full == false){
			island_core[1] = (Math.random() * 100);
			//Upper/lower boundaries for island core expectation
			//Change higher or lower based on how close islands get to the shore
			var core_upper_limit = Math.round(island_core[1] + 50);
			//Check to make sure that top-sector lower values don't go below 0:
			var core_lower_limit;
			if (island_core[1] < 50){
				core_lower_limit = Math.round(island_core[1]);
			}
			else{
				core_lower_limit = Math.round(island_core[1] - 50);
			}
			island_core[0] = (Math.random() * 100);
			water_top_full = true;
			console.log("Assigned sector top");
			//return island_core;
		}
		else if (water_top_full == true){
			assign_island_sector();
		}
	}
	else if ((island_sector_distributor > (1/3)) && (island_sector_distributor <= (2/3))){
		if (water_mid_full == false){
			island_core[1] = (100 + (Math.random() * 100));
			//Upper/lower boundaries for island core expectation
			//Change higher or lower based on how close islands get to the shore
			var core_upper_limit = Math.round(island_core[1] + 50);
			var core_lower_limit = Math.round(island_core[1] - 50);
			island_core[0] = (Math.random() * 100);
			water_mid_full = true;
			console.log("Assigned sector mid");
			//return island_core;
		}
		else if (water_mid_full == true){
			assign_island_sector();
		}
	}
	else if (island_sector_distributor > (2/3)){
		if (water_bottom_full == false){
			island_core[1] = (200 + (Math.random() * 100));
			core_upper_limit = Math.round(island_core[1] + 50);
			var core_lower_limit = Math.round(island_core[1] - 50);
			island_core[0] = (Math.random() * 100);
			water_bottom_full = true;
			console.log("Assigned sector bottom");
			//return island_core;
		}
		else if (water_bottom_full == true){
			assign_island_sector();
		}
	}

	console.log("Island core: " + island_core[0] + ", " + island_core[1]);
	
	return island_core;
}

for (var i = 0; i < 3; i++){
	
	console.log("ASSIGNING ISLAND " + (i + 1));
	var island_center = assign_island_sector();
	for (var p = 0; p < 2; p++){
		console.log(island_center[p]);
	}
}

编辑:添加我调用该函数的地方,抱歉。所有这一切都将返回的函数值(应该是带有x和y坐标的数组)分配到一个新变量中,然后我继续使用它。

1 个答案:

答案 0 :(得分:0)

代码对我来说很困惑。

undefied中的console.log是因为您没有将递归调用的返回设置为island_core变量 - 所以它在第一次打印正常并打印{{ 1}}第二次。

尝试这样做:

undefined

<强> EDITED

island_core = assign_island_sector();