Home > Software engineering >  array of numbers that starts from 0 and ends with the number equal to the number length of n
array of numbers that starts from 0 and ends with the number equal to the number length of n

Time:10-20

How can I create an array of numbers that starts from 0 and ends with the number equal to the number length of n?

ex> n=1 output: 123456789 n=2 output: 12345...99

CodePudding user response:

Finished version of @JavaScript suggestion in the comments.

  1. Generate an empty array using Math.pow
  2. Remove the last item since we do not display 0
  3. Use map to return an array with the index parameter plus 1 (=no 0).

/***
* n=number of digits
*/
function xyz(n){
  return Array.from({length: Math.pow(10, n) - 1}, (value, index) => index   1)
};

console.log(xyz(1))
console.log(xyz(2))

Edited due to the comments provided.

CodePudding user response:

Simple loop

Determine the max value which is where each digit is a nine and there as many digits as n. Then loop and create an array with the values:

function fn(n) {
  const min = 1;
  const max = Number("9".repeat(n));
  
  const arr = [];
  for(let i = min; i <= max; i  )
    arr.push(i);
    
  return arr;
}

const singleDigit = fn(1);
const twoDigit = fn(2);

console.log(JSON.stringify(singleDigit));
console.log(JSON.stringify(twoDigit));
<h1>See browser console for better view of the logged values</h1>

Using generators and iterators

A more generic solution can be done using generators. A single generate() function can handle creating an infinite amount of digits. This can then be limited to an upper bound with generic utility functions. This creates an iterator that will supply values from min to max and this can be converted to an array using Array.from().

Some implementations:

take

Take a set amount of items from an iterator:

function* generate(start, step = 1) {
  for(let i = start;; i = step)
    yield i;
}

function* take(num, iterator) {
  for (const item of iterator) {
    if (--num < 0)
      break;
      
    yield item;
  }
}

function fn(n) {
  const min = 1;
  const max = Number("9".repeat(n));
  
  const iterator = take(
    max,
    generate(min)
  );
  
  return Array.from(iterator);
}

const singleDigit = fn(1);
const twoDigit = fn(2);

console.log(JSON.stringify(singleDigit));
console.log(JSON.stringify(twoDigit));
<h1>See browser console for better view of the logged values</h1>

takeUntil

Take items from an iterator until a condition is satisfied then stop:

function* generate(start, step = 1) {
  for(let i = start;; i = step)
    yield i;
}

function* takeUntil(predicate, iterator) {
  for (const item of iterator) {
    yield item;
    if (predicate(item))
      break;
  }
}

function fn(n) {
  const min = 1;
  const max = Number("9".repeat(n));
  
  const iterator = takeUntil(
    x => x >= max,
    generate(min)
  );
  
  return Array.from(iterator);
}

const singleDigit = fn(1);
const twoDigit = fn(2);

console.log(JSON.stringify(singleDigit));
console.log(JSON.stringify(twoDigit));
<h1>See browser console for better view of the logged values</h1>

takeWhile

Only take values from an iterator while a condition is satisfied, stop as soon as it is not:

function* generate(start, step = 1) {
  for(let i = start;; i = step)
    yield i;
}

function* takeWhile(predicate, iterator) {
  for (const item of iterator) {
    if (!predicate(item))
      break;
      
    yield item;
  }
}

function fn(n) {
  const min = 1;
  const max = Number("9".repeat(n));
  
  const iterator = takeWhile(
    x => x <= max,
    generate(min)
  );
  
  return Array.from(iterator);
}

const singleDigit = fn(1);
const twoDigit = fn(2);

console.log(JSON.stringify(singleDigit));
console.log(JSON.stringify(twoDigit));
<h1>See browser console for better view of the logged values</h1>

For more information

See on MDN:

CodePudding user response:

Are you asking for something that goes like:

1,2,3,4,5,6,7,8,9,10,11 ? I may have an answer if yes

CodePudding user response:

var lst[]; // the array
var nprime = 0;
while (x < n) {
lst.push(nprime);
nprime  ;
}
  • Related