Is there a way to generate sequence of characters or numbers in javascript?
For example, I want to create array that contains eight 1s. I can do it with for loop, bu
One liner:
new Array(10).fill(1).map( (_, i) => i+1 )
Yields:
[ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ]
This is a good option
var result = [];
for (var i = 1; i != 4; ++i) result.push(i)
check here for more options https://ariya.io/2013/07/sequences-using-javascript-array
A sequence is a stream, which computes the value when it is needed. This requires only a bit memory but more CPU time when the values is used.
An array is a list of precomputed values. This takes some time before the first value can be used. And it requires much memory, because all possible values of the sequence must be stored in memory. And you have to define an upper limit.
This means, that in most cases it is no good idea to create an array with sequence values. Instead it is better to implement the sequence as a real sequence, which is limited just by the word length of the CPU.
function make_sequence (value, increment)
{
if (!value) value = 0;
if (!increment) increment = function (value) { return value + 1; };
return function () {
let current = value;
value = increment (value);
return current;
};
}
i = make_sequence()
i() => 0
i() => 1
i() => 2
j = make_sequence(1, function(x) { return x * 2; })
j() => 1
j() => 2
j() => 4
j() => 8
range(start,end,step)
: With ES6 IteratorsYou can easily create range()
generator function which can function as an iterator. This means you don't have to pre-generate the entire array.
function * range ( start, end, step ) {
let state = start;
while ( state < end ) {
yield state;
state += step;
}
return;
};
Now you may want to create something that pre-generates the array from the iterator and returns a list. This is useful for functions that accept an array. For this we can use Array.from()
const generate_array = (start,end,step) => Array.from( range(start,end,step) );
Now you can generate a static array easily,
const array = generate_array(1,10,2);
But when something desires an iterator (or gives you the option to use an iterator) you can easily create one too.
for ( const i of range(1, Number.MAX_SAFE_INTEGER, 7) ) {
console.log(i)
}
Generating an integer sequence is something that should definitely be made more convenient in JavaScript. Here is a recursive function returns an integer array.
function intSequence(start, end, n = start, arr = []) {
return n === end ? arr.concat(n)
: intSequence(start, end, start < end ? n + 1 : n - 1, arr.concat(n));
}
$> intSequence(1, 1)
<- Array [ 1 ]
$> intSequence(1, 3)
<- Array(3) [ 1, 2, 3 ]
$> intSequence(3, -3)
<- Array(7) [ 3, 2, 1, 0, -1, -2, -3 ]
The original question was edited. So the updated example answers:
To fill the same content:
Array(8).fill(1)
//=> [1, 1, 1, 1, 1, 1, 1, 1]
To fill sequential numbers, starting from 5:
Array(8).fill().map((element, index) => index + 5)
//=> [5, 6, 7, 8, 9, 10, 11, 12]
To fill sequencial characters, starting from 'G':
Array(8).fill().map((element, index) => String.fromCharCode('G'.charCodeAt(0) + index))
//=> ["G", "H", "I", "J", "K", "L", "M", "N"]