About Pi Downloads Documentation Plugins Developer Forum Issues

Number.range

Summary

Generates lists containing arithmetic progressions.

Syntax

Number.range(Callback (Optional))

Example

>>> (5).range()
[0,1,2,3,4]
>>> 3.8.range()
[3,4,5,6,7]
>>> 3.8.range(function(i){ return i*2 })
[6, 8, 10, 12, 14]

Examples

Example #1: Print char codes of 'Ring' string.

Source files of the above example:

../../examples/charCodes.js

var str = "Ring";
str.length.range().forEach(function(i){
	document.write( " <b>" + str.substring(i,i+1) + "</b> " + str.charCodeAt(i) + "<br />" )
});

Example #2: Change background color of the boxes

Source files of the above example:

../../examples/box.html

<div id="container"><div class="box"></div><div class="box"></div><div class="box"></div><div class="box"></div></div>

../../examples/box.css

#container { height:110px; }
#container .box { float:left; width:100px; height:100px; margin:3px; border:1px solid #000; background:#fff; }

../../examples/box.js

pi.get('container').childNodes.length.range().forEach(function(i){
	pi.get('container').childNodes[i].style.background = "rgb({0},{1},{2})".format(
		pi.util.Number.random(0,255),
		pi.util.Number.random(0,255),
		pi.util.Number.random(0,255)
	);
})

See Also