如何从另一个函数获取循环数字

时间:2018-06-07 12:30:28

标签: javascript

我希望得到介于1到25之间的数字。使用第一个函数,我得到所有数字在1到25之后,我想在第二个函数中获取所有这些数字,并将它们中的每一个放在单独的td中。现在它未定义,我无法从第一个函数中获取数字。



function range(arr1, arr2){
	var arr = [];
	for( var i = arr1; i <= arr2; i++ ){
		arr.push(arr2[i]);
	}
	return arr;
}

function createTable(arr){
	// var arr = [];
	var table = document.createElement("table");
	for( var i = 0; i < arr.length; i++ ){
		var td = document.createElement("td");
		td.innerHTML = arr[i];
		document.body.appendChild(table);
		table.appendChild(td);
	}
	return;

}
createTable(range(1, 25));
&#13;
td{
  border: 1px solid green;

}
&#13;
&#13;
&#13;

3 个答案:

答案 0 :(得分:3)

import torch w = torch.tensor(1.0) w.requires_grad_(True) # sub-graph for calculating `x` x = w+10 # sub-graph for calculating `y1` x1 = x*x y1 = x1*x1 y1.backward(retain_graph=True) # all graph is retained # remove unneeded parts of graph. Note that these parts will be freed from memory (even if they were on GPU), due to python's garbage collection y1 = None x1 = None # sub-graph for calculating `y2` x2 = torch.sqrt(x) y2 = x2/2 y2.backward() 功能

中的arr.push(arr2[i]);更改为arr.push(i);

range是一个数字,因此arr2始终未定义

arr2[i]
function range(arr1, arr2){
	var arr = [];
	for( var i = arr1; i <= arr2; i++ ){
		arr.push(i);
	}
	return arr;
}

function createTable(arr){
	// var arr = [];
	var table = document.createElement("table");
	for( var i = 0; i < arr.length; i++ ){
		var td = document.createElement("td");
		td.innerHTML = arr[i];
		document.body.appendChild(table);
		table.appendChild(td);
	}
	return;

}
createTable(range(1, 25));

答案 1 :(得分:1)

在范围功能中进行以下更改: 按(i)而不是按(arr2 [i])

  id   exchange       created_at            updated_at        deleted_at   start_time   close_time  
 ---- ---------- --------------------- --------------------- ------------ ------------ ------------ 
   1   NSE        2018-04-18 06:22:11   2018-04-18 06:22:11   (NULL)       09:15:00     03:30:00    
   2   BSE        2018-05-06 07:07:53   2018-05-06 07:07:53   (NULL)       00:00:00     00:00:00    
   3   NYSE       2018-05-19 08:34:07   2018-05-19 08:34:07   (NULL)       00:00:00     00:00:00    

答案 2 :(得分:0)

您的range()函数使用arr2作为数组(arr.push(arr2[i]))和数字。

你可以像这样重写它:

&#13;
&#13;
function range(lowerBound, upperBound) {
    return [...Array(upperBound - lowerBound + 1).keys()].map((x) => (x + lowerBound));
}
&#13;
&#13;
&#13;